Frequently Used javascript verification function collection _ javascript skills

Source: Internet
Author: User
We recommend that you collect the javascript verification functions that are frequently used. You can use them directly in the future.
/*
========================================================== ======================================

Whether it is null. Only the null string or the length of 0 is null. After trim IsStringNull (string)

========================================================== ======================================

*/

function IsStringNull(str) {     if (str == null)         return true;     var trimStr = Trim(str);     if (trimStr.length == 0)         return true;     return false; }

/*

========================================================== ======================================

LTrim (string): removes spaces on the left.

========================================================== ======================================

*/

function LTrim(str) {     var whitespace = new String(" \t\n\r");     var s = new String(str);     if (whitespace.indexOf(s.charAt(0)) != -1) {         var j = 0, i = s.length;         while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {             j++;         }         s = s.substring(j, i);     }     return s; }

/*

========================================================== ======================================

RTrim (string): removes spaces on the right.

========================================================== ======================================

*/ function RTrim(str) {     var whitespace = new String(" \t\n\r");     var s = new String(str);     if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {         var i = s.length - 1;         while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {             i--;         }         s = s.substring(0, i + 1);     }     return s; } /*

========================================================== ======================================

Trim (string): removes leading and trailing spaces.

========================================================== ======================================

*/ function Trim(str) {     return RTrim(LTrim(str)); } /*

========================================================== ======================================

IsOutOfLength (string, int): determines whether the length of a string exceeds the length. The Chinese character contains 2 characters.

========================================================== ======================================

*/ function IsOutOfLength(str, len) {     var strLength = 0;     for (var i = 0; i < str.length; i++) {         if (str.charCodeAt(i) > 256) {             strLength++;         }         strLength++;         if (strLength > len) {             return true;         }     }     return false; } /*

========================================================== ======================================

IsOutOfLength (string, int): determines whether the length of a string exceeds the length. The Chinese character contains three characters.

========================================================== ======================================

*/ function IsOutOfLength3(str, len) {     var cArr = str.match(/[^\x00-\xff]/ig);     var len_address = str.length + (cArr == null ? 0 : cArr.length * 2);     if (len_address > len)         return true;     else         return false; } /*

========================================================== ======================================

IsNumeric (string): determines whether the string is a number.



========================================================== ======================================

*/ function IsNumeric(strNumber) {     if (strNumber.length == 0) {         return false;     }     return (strNumber.search(/^(-|\+)?\d+(\.\d+)?$/) != -1); } /*

========================================================== ======================================

IsInt (string, string, int or string) :( test string, + or-or empty, empty or 0) function: judge whether it is an integer, positive integer, negative integer, positive integer + 0, negative integer + 0

========================================================== ======================================

*/Function IsInt (objStr, sign, zero) {var reg; var bolzero; if (Trim (objStr) = "") {return false;} else {objStr = objStr. toString () ;}if (sign = null) | (Trim (sign) = "") {sign = "+ -";} if (zero = null) | (Trim (zero) = "") {bolzero = false;} else {zero = zero. toString (); if (zero = "0") {bolzero = true;} else {alert ("check whether the parameter 0 is included, can only be (null, 0) ") ;}} switch (sign) {case" none ": I F (! Bolzero) {reg =/^ [0-9] * [1-9] [0-9] * $ /;} else {reg =/^ [0-9] * [0-9] [0-9] * $/;} break; case "+ -": // integer reg =/(^ -? | ^ \ + ?) \ D + $/; break; case "+": if (! Bolzero) {// positive integer reg =/^ \ +? [0-9] * [1-9] [0-9] * $/;} else {// positive integer + 0 // reg =/^ \ +? \ D + $/; reg =/^ \ +? [0-9] * [0-9] [0-9] * $/;} break; case "-": if (! Bolzero) {// negative integer reg =/^-[0-9] * [1-9] [0-9] * $ /;} else {// negative integer + 0 // reg =/^-\ d + $ /; reg =/^-[0-9] * [0-9] [0-9] * $/;} break; default: alert ("Check symbol parameters, only (null, +,-) "); return false; break;} var r = objStr. match (reg); if (r = null) {return false;} else {return true ;}}/*

========================================================== ======================================

CheckIsValidDate (string): determines whether the date type is correct. Must be yyyy-MM-dd

========================================================== ======================================

*/Function checkIsValidDate (str) {// if it is null, if (str = "") return true is verified; var pattern =/^ \ d {4} \/\ d {1, 2} \/\ d {1, 2} $/g; if (! Pattern. test (str) return false; // alert ("[" + str + "] 1"); var arrDate = str. split ("/"); var date = new Date (arrDate [0], (parseInt (arrDate [1], 10)-1) + "", parseInt (arrDate [2], 10) + ""); // alert ("a: [" + date. getFullYear () + "] [" + date. getMonth () + "] [" + date. getDate () + "]"); // alert ("B: [" + arrDate [0] + "] [" + parseInt (arrDate [1], 10) + "] [" + parseInt (arrDate [2], 10) + "]"); if (date. getFullYear () = arrDate [0] & date. getMonth () = (parseInt (arrDate [1], 10)-1) + "" & date. getDate () = parseInt (arrDate [2], 10) + "") return true; else // alert ("[" + str + "] 2 "); return false ;}/*

========================================================== ======================================

CheckIsValidTime (string): determines whether the time type is correct. Must be hh: mm: ss

========================================================== ======================================

*/Function checkIsValidTime (str) {// if it is null, verify if (str = "") return true; var pattern =/^ \ d {1, 2 }: \ d {1, 2 }:\ d {1, 2} $/g; if (! Pattern. test (str) return false; // alert ("[" + str + "] 1"); return true ;}/*

========================================================== ======================================

CheckedCount (containForm, chkFormName): calculates the number of phases selected in a form. check forms include radiobox and checkbox parameters: form containing check items, and name of check Forms

========================================================== ======================================

*/Function CheckedCount (containForm, chkFormName) {var chkCount = 0; for (I = 0; I <containForm. elements. length; I ++) {if (containForm. elements [I]. name = chkFormName) {if (containForm. elements [I]. type = 'checkbox' | containForm. elements [I]. type = 'Radio ') {if (containForm. elements [I]. checked) {chkCount ++ ;}}} return chkCount ;}/ *** determines whether the email address is valid */function IsValidateEmail (str ){ // If it is null, if (str = "" | str. length = 0) {return false;} // Regular Expression // var pattern =/^ \ w {1 ,}@[\., \ w] {1 ,}$/; var pattern =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $/; if (! Pattern. test (str) {return false;} return true;}/*** determines whether a valid Chinese character */function checkIsHanzi (str) {// if it is null, if (str = "" | str. length = 0) {return true;} // Regular Expression var pattern =/[^ \ u4E00-\ u9FA5]/g; if (pattern. test (str) {return false;} return true;}/*** determines whether the English letter is valid + (Space) */function checkIsLetter (str) {// if it is null, if (str = "" | str. length = 0) {return true;} // Regular Expression var patt Ern =/[^ a-zA-Z \ s]/g; if (pattern. test (str) {return false;} return true;}/*** determines whether the English letter is valid + (space or dot) */function checkIsLetterOrSpaceDot (str) {// if it is null, if (str = "" | str. length = 0) {return true;} // Regular Expression var pattern =/[^ a-zA-Z \ s \.] /g; if (pattern. test (str) {return false;} return true;}/*** determines whether a valid English letter or number */function checkIsLetterNumber (str) {// if it is null, if (str = "" | str. Length = 0) {return true;} // Regular Expression var pattern =/[^ a-zA-Z0-9 \ s]/g; if (pattern. test (str) {return false;} return true;}/*** determines whether the digit is a valid number (check the credential number and password) */function checkIsNumber (str) {// if it is null, if (str = "" | str. length = 0) {return true;} // Regular Expression var pattern =/[^ 0-9 \ s]/g; if (pattern. test (str) {return false;} return true;}/*** determines whether the percentage number is valid */function checkIsPercent (str ){/ /If it is null, if (str = "" | str. length = 0) {return true;} // Regular Expression var pattern =/^ [1-9] [0-9] * % $/g; if (! Pattern. test (str) {return false;} return true;}/*** check is validate time */function isValidateTime (str) {if (parseInt (str) = 0) {return true;} var regexp =/^ ([0-9]) | (0 [0-9]) | (1 [0-9]) | (2 [0-3]) [0-5] [0-9] $/if (str = "" | str. length = 0) {return false;} if (! Regexp. test (str) {return false;} return true;}/*** determines whether the mobile phone number is valid *. return true if the format is correct; otherwise, false. */function IsValidateMobile (str) {var pattern =/^ (\ d {2, 3} \) | (\ d {3 }\-))? 13 \ d {9 }$/; if (str = ''| str. length = 0) {return false;} if (! Pattern. test (str) {return false;} return true;}/*** determines whether the phone number is valid. * If the phone number format is correct, return true. Otherwise, false. */function IsValidatePhone (str) {var pattern =/^ (\ d {2, 3} \) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} (\-\ d {1, 4 })? $/; If (str = ''| str. length = 0) {return false;} if (! Pattern. test (str) {return false;} return true;}/*** determines whether the zip code is valid. * If the format is correct, true is returned. Otherwise, false is returned. */function IsValidateZipcode (str) {var pattern =/^ [1-9] \ d {5} $/; if (str = ''| str. length = 0) {return false;} if (! Pattern. test (str) {return false;} return true ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.