Js regular expression to verify various requirements (integer, real number, decimal, time, URL, etc)

Source: Internet
Author: User
FunctionvalidateNum (val) {// verify the integer varpatten/^ -? \ D + $/; returnpatten. test (val);} functionvalidateRealNum (val) {// verify the real number varpatten/^ -? \ D + \.? \ D * $/; returnpatten. test (val);} fu

Function validateNum (val) {// verify the integer
Var patten =/^ -? \ D + $ /;
Return patten. test (val );
}
Function validateRealNum (val) {// verify the real number

Var patten =/^ -? \ D + \.? \ D * $ /;

Return patten. test (val );

}

Function validateNum (val) {// verify the decimal point, retain one decimal point

Var patten =/^ -? \ D + \.? \ D {0, 1} $ /;

Return patten. test (val );

}

 
Function validateFloat (val) {// verify Decimals
Var patten =/^ -? (? : \ D + | \ d {1, 3 }(? :, \ D {3}) + )(? : \. \ D + )? $ /;
Return patten. test (val );
}
 
Function validateNumOrLetter (val) {// only numbers and letters can be entered
Var patten =/^ [A-Za-z0-9] + $ /;
Return patten. test (val );
}
 
Function validateColor (val) {// verify the color
Var patten =/^ # [0-9a-fA-F] {6} $ /;
Return patten. test (val );
}
 
Function validateUrl (val) {// verify the URL
Var patten =/^ (https? | Ftp): \ // ([a-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [! \ $ & '\ (\) \ * \ +,; =] | :)*@)? (\ D | [1-9] \ d | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d | [1-9] \ d | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d | [1-9] \ d | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d | [1-9] \ d | 1 \ d | 2 [0-4] \ d | 25 [0-5]) | ([a-z] | \ d | [\ u00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | ([a-z] | \ d | [\ u00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) ([a-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) * ([a-z] | \ d | [\ u00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) \.) + ([a-z] | [\ u00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | ([a-z] | [\ u00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) ([a-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) * ([a-z] | [\ u00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) \.?) (: \ D *)?) (\/([A-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [! \ $ & '\ (\) \ * \ +,; =] |: | @) + (\/([A-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [! \ $ & '\ (\) \ * \ +,; =] |: | @)*)*)?)? (\? ([A-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [! \ $ & '\ (\) \ * \ +,; =] |: | @) | [\ UE000-\ uF8FF] | \/| \?) *)? (# ([A-z] | \ d |-| \. | _ | ~ | [\ U00A0-\ uD7FF \ uF900-\ uFDCF \ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [! \ $ & '\ (\) \ * \ +,; =] |: | @) | \/| \?) *)? $/I;
Return patten. test (val );
}
 
Function validateNull (val) {// verification is empty
Return val. replace (/\ s +/g, ""). length = 0;
}
 
Function validateDate (val) {// verification time 2010-10-10
Var patten =/^ \ d {4} [\/-] \ d {1, 2} [\/-] \ d {1, 2} $ /;
Return patten. test (val );
}
 
Function validateNumLetterLine (val) {// only numbers, letters, and underscores can be entered
Var patten =/^ [a-zA-Z0-9 _] {1,} $ /;
Return patten. test (val );
}


Author: uohzoaix
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.