javascript工具類 (判斷閏年,漢字,IP,精度等。。)

來源:互聯網
上載者:User
去除首尾空格,檢查字串是否包含漢字,開啟新視窗,驗證ip,將浮點數規整為指定的精度,判斷提供的字串是否為空白,判斷提供的字串是否為整數,判斷輸入的字元是否為大寫或字母,判斷輸入的字元是否為0-9數字字元,判斷提供的字串只含有數字字元,判斷閏年)
/************************************************************************************名稱:existChinese(strValue)功能:該函數用於檢查字串是否包含漢字參數:strValue---入參;字串;待處理的字串返回:布爾值; true--包含漢字; false--不包含漢字引用:無說明:**************************************************************************************/function existChinese(strValue){var chrCodefor(var iChar = 0; iChar < strValue.length; iChar++){chrCode = strValue.charCodeAt(iChar);if(parseInt(chrCode) > 255){return true;}}return false;}
/******************************************************************************** ** * 開啟新視窗 */function openWindow(i_URL, i_Width, i_Height) {    openWindow(i_URL, i_Width, i_Height, null, null);}function openWindow(i_URL, i_Width, i_Height, i_Feature) {    openWindow(i_URL, i_Width, i_Height, null, i_Feature);}function openWindow(i_URL, i_Width, i_Height, i_WindowName, i_Feature){    var v_URL = i_URL;    var v_Width = i_Width;    var v_Height = i_Height;    var v_WindowName = i_WindowName;    var v_Top;    var v_Left;    var v_Feature = "";    if (v_WindowName == null) {        v_WindowName = "myWindow";    }    var objWindow    if (!objWindow || objWindow.closed)    {        v_Top = screen.availHeight / 2 - v_Height / 2 - 30;        v_Left = screen.availWidth / 2 - v_Width / 2;        v_Feature += "top=" + v_Top + ",";        v_Feature += "left=" + v_Left + ",";        v_Feature += "width=" + v_Width + ",";        v_Feature += "height=" + v_Height + ",";        if (i_Feature == null) {            v_Feature += "directories=no,fullscreen=no,resizable=no,scrollbars=yes,status=1";        } else {            v_Feature += i_Feature;        }        objWindow = window.open(v_URL, v_WindowName, v_Feature);        //objWindow=window.open (v_URL,v_WindowName,"directories=no,fullscreen=no,resizable=no,scrollbars=no,status=1");        //v_Top=screen.availHeight/2-v_Height/2;        //v_Left=screen.availWidth/2-v_Width/2;        //objWindow.moveTo(v_Left,v_Top);        //objWindow.resizeTo(v_Width,v_Height);        objWindow.outerWidth = screen.availWidth;        objWindow.outerHeight = screen.availHeight - 25;    } else {        objWindow.focus();    }    objWindow.focus();}
/************************************************************************************名稱: trim(strValue)功能: 該函數用於去除字串前後的空格參數: strValue---入參;字串;待處理的字串返回: 字串引用: 無說明: 若為全空白字元串則返回空**************************************************************************************/function trim(strValue){var iLTR, jRTL;var chr;//去除字串前後的空格for( iLTR = 0; iLTR < strValue.length; iLTR++ ){chr = strValue.charAt(iLTR) ;if( chr != " " ) break;}if( iLTR == strValue.length ) return "";//去除字串前後的空格for( jRTL = strValue.length - 1; jRTL >= 0; jRTL-- ){chr = strValue.charAt(jRTL);if( chr != " " ) break;}return strValue.substring(iLTR, jRTL + 1);}
/* * 驗證ip */function checkIp(strIP) {    if (isEmpty(strIP)) return false;        var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的Regex    if (re.test(strIP))    {        if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) return true;    }    return false;}
/** * 將浮點數規整為指定的精度 * @pram original_number 原始浮點數 * @pram decimals 結果的小數位精度值 * @return 規整後的浮點數 */function round_decimals(original_number , decimals){var result1 = original_number * Math.pow(10 , decimals);var result2 = Math.round(result1);var result3 = result2 / Math.pow(10 , decimals);return(result3);}
/** * 判斷提供的字串是否為空白 * @param field 輸入字串 * @return true/false */function isEmpty(field){return ((field == null) || (field.length == 0) || myTrim(field)=="");}/** * 判斷提供的字串是否為整數 * @param field 輸入字串 * @return true/false */function isInteger(field){s = myTrim(field);var i;if (isEmpty(field)){return false;}for (i=0; i<field.length; i++){var c = field.charAt(i);if (!isDigit(c)){return false;}if(c==0&&i==0&&field.length>1){return false;}}return true;}/** * 判斷輸入的字元是否為大寫或字母字元 * @param c 輸入字元 * @return true/false */function isLetter(c){return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );}/** * 判斷輸入的字元是否為0-9數字字元 * @param c 輸入字元 * @return true/false */function isDigit(c){return ((c >= "0") && (c <= "9"));}
/** * 判斷提供的字串只含有數字字元 * @param field 輸入字串 * @return true/false */function isNumbers(field){field = myTrim(field);var i;for (i = 0; i < field.length; i++){var c = field.charAt(i);if (!isDigit(c) ){return false;}}return true;}
/***判斷閏年* @param s 年 @return true/false**/function isRunNian(s){//alert(s%4);if(s%4!=0){return false;}else{if(s%100!=0){return true;}else{if(s%400==0){return true;}else{return false;}}}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.