Collecting 28 JS verification functions

Source: Internet
Author: User

JS verification function call method and note:
1. Set onsubmit = "return test ()" for the form and submit it with <input type = "Submit" name = "submit" value = "check">.
2. <input type = text onkeydown = "test ();"> // call function verification directly when you press the mouse
3. <input type = text onblur = "testl (this. value)"> // pass in your own value for verification, test (this) // pass in the object itself for verification
4. <input type = "button" value = "test" onclick = "test ()"> // click Btn for verification. If yes, use document. forms [0]. submit (); submit the form
5. <input onClick = "return test ();" type = "submit" name = "submit" value = "submit information"> // use submit for submission and verification, if return false in test, return true if not.
6. <script type = "javascript">... function test () {... return false; ......}... </script>
7. | and, & or ,! Non
8. common events: onblur loses focus, onchange loses focus, and content changes. The onfocus element obtains focus. onreset triggers this event when the RESET attribute in the form is fired and the onsubmit form is submitted.
9. Verify the regular expression: if (/^ [1-9] \ d * $/. test (str) returns true, pass, and FALSE
10.doc ument. getElementById ("ip"). value // The value of the access ID, which is accessed by document. form1.text1. value // by name
11. <input onblur = "if (this. value. replace (/^ + | + $/g,'') = '') alert ('cannot be blank! ') "> // Execute a single regular expression verification example
Verification function:
<Input type = "text" name = "text1" id = "text1" onblur = "test ();" value = ""/>
1. Character length limit
Function test ()
{
If (document. form1.text1. value. length> 50)
{
Alert ("cannot exceed 50 characters! ");
Document. form1.text1. focus ();
Return false;
}
}
2. Only English letters, letters, or numbers are allowed.
Function test ()
{
If (! (Event. keyCode> = 65 & event. keyCode <= 90 ))
{
Alert ("only English! ");
Document. form1.text1. focus ();
}
}
<Input onblur = "if (/[^ 0-9a-zA-Z]/g. test (this. value) alert ('error')"> // letters or numbers
3. Only numbers are allowed.
Function test ()
{
If (! (Event. keyCode> = 48 & event. keyCode <= 57) | (event. keyCode> = 96 & event. keyCode <= 105) // consider the number key on the keypad
{
Alert ("only numbers! ");
Document. form1.text1. focus ();
}
}
Or
Function test (NUM)
{
Var I, j, strTemp;
StrTemp = "0123456789 ";
If (NUM. length = 0)
Return 0
For (I = 0; I <NUM. length; I ++)
{
J = strTemp. indexOf (NUM. charAt (I ));
If (j =-1)
{
Alert ("only numbers! ");
Return false;
}
}
// The description is a number.
Return true;
}
5. Verify the email address (Regular Expression, function)
Function isEmail (strEmail ){
If (strEmail. search (/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /)! =-1)
Return true;
Else
Alert ("format error! ");
}
Function isEmail (){
If (document. userinfo. useremail. value. charAt (0) = "." |
Document. userinfo. useremail. value. charAt (0) = "@" |
Document. userinfo. useremail. value. indexOf ('@', 0) =-1 |
Document. userinfo. useremail. value. indexOf ('.', 0) =-1 |
Document. userinfo. useremail. value. lastIndexOf ("@") = document. userinfo. useremail. value. length-1 |
Document. userinfo. useremail. value. lastIndexOf (".") = document. userinfo. useremail. value. length-1)
{
Alert ("Incorrect Email address format! ");
Document. userinfo. useremail. focus ();
Return false;
}
}
<Input type = "text" onblur = "isEmail (this. value);" name = "text1"/>
6. Shielding keywords (here, shielding ***** and ****)
Function test (){
If (document. form1.text1. value. indexOf ("***") = 0) | (document. form1.text1. value. indexOf ("***") = 0 )){
Alert ("keyword ");
Document. form1.text1. focus ();
Return false;
}
}
7. Compare whether the two inputs are the same
If (document. userinfo. userpassword. value! = Document. userinfo. userpassword1.value ){
Document. userinfo. userpassword. focus ();
Document. userinfo. userpassword. value = '';
Document. userinfo. userpassword1.value = '';
Alert ("the two passwords are different. Please try again! ");
Return false;
}
8. Determine whether it is composed of null or spaces
Function test (){
If (checkspace (document. form1.text1. value )){
Document. form1.text1. focus ();
Alert ("is empty or contains spaces! ");
Return false;
}
}
Function checkspace (checkstr ){
Var str = '';
For (I = 0; I <checkstr. length; I ++ ){
Str = str + '';
}
Return (str = checkstr );
}
Or:
<Input onblur = "if (this. value. replace (/^ + | + $/g,'') = '') alert ('cannot be blank! ') "> // Execute a single regular expression verification example
9. verify whether the phone number is a digital phone. It can only be a number or-
Regular: \ d {3}-\ d {8} | \ d {4}-\ d {7}
Function istel (elem ){
Var str = elem. value;
Var oneDecimal = false;
Var oneChar = 0;
Str = str. toString ();
For (var I = 0; I <str. length; I ++ ){
OneChar = str. charAt (I). charCodeAt (0 );
If (oneChar = 45) {continue ;}
If (oneChar <48 | oneChar> 57 ){
Alert ("this option can only contain numbers and .");
Return false;
}
}
Return true;
}
Or
Function fucCheckTEL (TEL)
{
Var I, j, strTemp;
StrTemp = "0123456789 -()#";
For (I = 0; I <TEL. length; I ++)
{
J = strTemp. indexOf (TEL. charAt (I ));
If (j =-1)
{
Alert ("this option can only contain numbers and .");
Return false;
}
}
// Valid description
Return true;
}
Or
// Verify the phone number and fax number. The phone number can start with "+" and contain "-" in addition to numbers.
Function isTel (s)
{
// Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) {1, 12}) + $ /;
Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe c (s) return false
Return true
}
Or
Requirements:
(1) Telephone Numbers consist of numbers, "(", ")", and "-"
(2) 3 to 8 phone numbers
(3) If the telephone number contains a district code, the district code is three or four digits.
(4) area codes are separated by "(", ")" or "-" and other sections.
(5) the mobile phone number is 11 or 12 digits. If the mobile phone number is 12 digits, the first digit is 0.
(6) The first and second digits of 11 mobile phone numbers are "13"
(7) The second and third digits of the 12-digit mobile phone numbers are "13"
Based on these rules, you can use the following regular expressions:
(^ [0-9] {3, 4} \-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ \ ([0-9] {3, 4} \) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} $)
Function PhoneCheck (s ){
Var str = s;
Var reg =/(^ [0-9] {3, 4} \-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ \ ([0-9] {3, 4} \) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} $ )/
Alert (reg. test (str ));
}
<Input type = "text" name = "text1" onblur = "istel (this);"/>
10. // when opt2 is 1, check if num is negative. // when opt1 is 1, check if num is decimal. // it is correct to return 1, and 0 is incorrect.
Function chknbr (num, opt1, opt2)
{
Var I = num. length;
Var staus;
// The number of staus records.
Status = 0;
If (opt2! = 1) & (num. charAt (0) = '-'))
{
Alert ("You have enter a invalid number .");
Return 0;
}
// An error occurs when the last digit is.
If (num. charAt (I-1) = '.')
{
Alert ("You have enter a invalid number .");
Return 0;
}
For (j = 0; j <I; j ++)
{
If (num. charAt (j) = '.')
{
Status ++;
}
If (status> 1)
{
Alert ("You have enter a invalid number .");
Return 0;
}
If (num. charAt (j) <'0' | num. charAt (j)> '9 ')
{
If (opt1 = 0) | (num. charAt (j )! = '.') & (J! = 0 ))
{
Alert ("You have enter a invalid number .");
Return 0;
}
}
}
Return 1;
}
11. Check whether the string is composed of digits or letters
Function test (str)
{
Var strSource = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
Var ch;
Var I;
Var temp;
For (I = 0; I <= (str. length-1); I ++)
{
Ch = str. charAt (I );
Temp = strSource. indexOf (ch );
If (temp =-1)
{
Alert ("format incorrect! ");
Return false;
}
}
If (strSource. indexOf (ch) =-1)
{
Alert ("format incorrect! ");
Return false;
}
Else
{
Return true;
}
}
12. digit Verification
Positive Integer verification ^ [1-9] \ d * $, negative integer verification ^-[1-9] \ d * $, integer ^ -? [1-9] \ d * $, non-negative integer ^ [1-9] \ d * | 0 $, non-positive number ^-[1-9] \ d * | 0 $, floating point number ^ -? ([1-9] \ d * \. \ d * | 0 \. \ d * [1-9] \ d * | 0? \. 0 + | 0) $
Function test (str ){
If (/^ [1-9] \ d * $/. test (str ))
{
Alert ("format not supported! ");
Return false;
}
Else
{
Return true;
}
}
13. ID card
Regular: \ d {15} | \ d {18}
14. IP Address
Regular: \ d + \. \ d +
15. Zip code
Regular: [1-9] \ d {5 }(?! \ D)
Function isPostalCode (s)
{
// Var patrn =/^ [a-zA-Z0-9] {3, 12} $ /;
Var patrn =/^ [a-zA-Z0-9] {} $ /;
If (! Patrn.exe c (s) return false
Return true
}
16. QQ number
Regular: [1-9] [0-9] {4 ,}
17. HTML tag
Regular: \ d {3}-\ d {8} | \ d {4}-\ d {7}
18. Valid color values
Function IsColor (color ){
Var temp = color;
If (temp = "") return true;
If (temp. length! = 7) return false;
Return (temp. search (// \ # [a-fA-F0-9] {6 }/)! =-1 );
}
19. Valid Link
Function IsURL (url ){
Var sTemp;
Var B = true;
STemp = url. substring (0, 7 );
STemp = sTemp. toUpperCase ();
If (sTemp! = "HTTP: //") | (url. length <10 )){
B = false;
}
Return B;
}
20. Whether the mobile phone number is valid
Function IsMobile (_ str ){
Var tmp_str = trim (_ str );
Var pattern =/13 \ d {9 }/;
Return pattern. test (tmp_str );
}
Or
// Verify the mobile phone number. It must start with a number and can contain "-" in addition to a number.
Function IsMobile (s)
{
Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe c (s) return false
Return true
}
21. Verify the IP address and ID card number (regular)
Function checkIP ()
{
Obj = document. getElementById ("ip"). value
// Ip address
// Var exp =/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) $ /;
// ID card
// Var exp =/\ B (((?! \ D) \ d + | 1 \ d | 2 [0-4] \ d | 25 [0-5]) (\ B | \.)) {4 }/
// Var exp =/^ (\ d {15} | \ d {17} [x0-9])/
// Var exp =/^ ([\ d] {15} | [\ d] {18} | [\ d] {17} [x | X]) $/
Var reg = obj. match (exp );
If (reg = null)
{
Alert ("the IP address is invalid! ");
}
Else
{
Alert ("the IP address is valid! ");
}
}
22. Verify Logon Name: You can enter only 5-20 strings starting with a letter, which can contain numbers, "_", and ".".
Function isRegisterUserName (s)
{
Var patrn =/^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {} $ /;
If (! Patrn.exe c (s) return false
Return true
}
23. Verify User name: only 1-30 strings starting with letters can be entered
Function isTrueName (s)
{
Var patrn =/^ [a-zA-Z] {1, 30} $ /;
If (! Patrn.exe c (s) return false
Return true
}
24. Password verification: only 6-20 letters, numbers, and underscores can be entered.
Function isPasswd (s)
{
Var patrn =/^ (\ w) {6, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}
25. Date and Time
A short time, such as (13:04:06)
Function isTime (str)
{
Var a = str. match (/^ (\ d {1, 2 })(:)? (\ D {1, 2}) \ 2 (\ d {1, 2}) $ /);
If (a = null) {alert ('input parameter is not in the timeformat '); return false ;}
If (a [1]> 24 | a [3]> 60 | a [4]> 60)
{
Alert ("Time Format incorrect ");
Return false
}
Return true;
}
Short date, such)
Function strDateTime (str)
{
Var r = str. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /);
If (r = null) return false;
Var d = new Date (r [1], r [3]-1, r [4]);
Return (d. getFullYear () = r [1] & (d. getMonth () + 1) = r [3] & d. getDate () = r [4]);
}
Long time, such as (13:04:06)
Function strDateTime (str)
{
Var reg =/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d }) (\ d {1, 2}) :( \ d {1, 2}) :( \ d {1, 2}) $ /;
Var r = str. match (reg );
If (r = null) return false;
Var d = new Date (r [1], r [3]-1, r [4], r [5], r [6], r [7]);
Return (d. getFullYear () = r [1] & (d. getMonth () + 1) = r [3] & d. getDate () = r [4] & d. getHours () = r [5] & d. getMinutes () = r [6] & d. getSeconds () = r [7]);
}
26. The judgment characters are all composed of letters a-Z or a A-Z
<Input onblur = "if (/[^ a-zA-Z]/g. test (this. value) alert ('error')">
27. The judgment character consists of letters, numbers, underscores, and periods. It must start with an underscore or letter.
<Input onblur = "if (/^ ([a-zA-z _] {1}) ([\ w] *) $/g. test (this. value) alert ('error') ">
28. The form value cannot be blank, cannot exceed imax characters, and cannot be less than imix characters. The input is a Chinese character.
Function isNull (elem ){
// Var pattern =/^ \ s + | \ s + $ /;
If (elem. replace (/(^ \ s + | \ s $)/g, "") = ""){
Return false;
} Else {
Return true;
}
}
Function imax (elem ){
If (elem. length> imax ){
Return false;
} Else {
Return true;
}
}
Function imix (elem ){
If (elem. lengthreturn false;
} Else {
Return true;
}
}
Function isChinese (elem ){
Var pattern =/[^ \ x00-\ xff]/g;
If (pattern. test (elem )){
// Contains Chinese Characters
Return false;
} Else {
// Does not contain Chinese Characters
Return true;
}
}

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.