Common JS Form Verification Code (including examples)

Source: Internet
Author: User

Copy codeThe Code is as follows: // ---------------- Author Teng -------------
// Verify whether it is null
Function check_blank (obj, obj_name ){
If (obj. value! = ''){
Return true;
} Else {
Alert (obj_name + "cannot be blank! ");
Obj. value = "";
Return false;
}
}

// Filter the length of input characters
Function check_str_len (name, obj, maxLength ){
Obj. value = obj. value. replace (/(^ \ s *) | (\ s * $)/g ,"");
Var newvalue = obj. value. replace (/[^ \ x00-\ xff]/g ,"**");
Var length11 = newvalue. length;
If (length11> maxLength ){
Alert (name + "cannot exceed" + maxLength + "characters! ");
Obj. value = "";
Obj. focus ();
}
}

// Verification can only be a number
Function checkNumber (obj ){
Var reg =/^ [0-9] + $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert ('only numbers can be entered! ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

// Verify the value range

Function check_num_value (obj_name, obj, minvalue, maxvalue ){
Var reg =/^ [0-9] + $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert (obj_name + 'can only enter numbers! ');
Obj. value = "";
Obj. focus ();
Return false;
} Else if (minvalue> obj. value | obj. value> maxvalue ){
Alert (the range of obj_name + "is" + minvalue + "-" + maxvalue + "! ");
Obj. value = "";
Obj. focus ();
Return false;
}

}

// Verification can only contain letters and numbers
Function checkZmOrNum (zmnum ){
Var zmnumReg =/^ [0-9a-zA-Z] * $ /;
If (zmnum. value! = ""&&! ZmnumReg. test (zmnum. value )){
Alert ("only letters or numbers can be entered. Please enter again ");
Zmnum. value = "";
Zmnum. focus ();
Return false;
}
}

// Verify the double precision number
Function check_double (obj, obj_name ){
Var reg =/^ [0-9] + (\. [0-9] + )? $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert (obj_name + 'must be a valid double-precision number ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

// Select all check boxes
Function checkboxs_all (obj, cName ){
Var checkboxs = document. getElementsByName (cName );
For (var I = 0; I <checkboxs. length; I ++ ){
Checkboxs [I]. checked = obj. checked;
}
}

// Verify the zip code
Function check_youbian (obj ){
Var reg =/^ \ d {6} $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert ('postal code format incorrect! ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

// Verify the email format
Function check_email (obj ){
Var reg =/^ [a-zA-Z0-9 _-] + (\. ([a-zA-Z0-9 _-]) +) * @ [a-zA-Z0-9 _-] + [.] [a-zA-Z0-9 _-] + ([.] [a-zA-Z0-9 _-] +) * $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Obj. select ();
Alert ('email format incorrect! ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

/* Verify the landline number
0 \ d {2, 3} represents the area code
[0 \ +] \ d {2, 3} represents the international area code
\ D {7, 8} represents 7-8 digits (phone number)
Correct format: area code-phone number-extension code (full | only phone number)
*/

Function check_phone (obj ){
Var reg =/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3 })-)? (\ D {7, 8}) (-(\ d {3 ,}))? $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert ('incorrect phone number format! ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

// Verify the mobile phone number (check the mobile phone number starting with 13, 15, and 18 !)
Function check_telephone (obj ){
Var reg =/^ [1] [358] \ d {9} $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert ('mobile Phone Number Format incorrect! ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

// Verify whether it is Chinese
Function isChinese (obj, obj_name ){
Var reg =/^ [\ u0391-\ uFFE5] + $ /;
If (obj. value! = ""&&! Reg. test (obj. value )){
Alert (obj_name + 'must be in Chinese! ');
Obj. value = "";
Obj. focus ();
Return false;
}
}

// Determine whether the browser is Internet Explorer

Function checkIsIE (){
If (-[1,]) {
Alert ("this is not an Internet Explorer! ");
} Else {
Alert ("this is IE browser! ");
}
}

// Verify whether the URL is correct
Function check_IsUrl (obj ){

}

// Check the time (compared with the current time)
Function checkDate (obj, obj_name ){
Var obj_value = obj. value. replace (/-/g, "/"); // replace the character into the standard format (check format: '2017-12-10 ')
// Var obj_value = obj. value. replace ("-", "/"); // replace the character into the standard format (the test format is '2017-12-10 ')
Var date1 = new Date (Date. parse (obj_value ));
Var date2 = new Date (); // obtain the Date of today
If (date1> date2 ){
Alert (obj_name + "cannot be later than the current time! ");
Return false;
}
}
/// Copyright 2010-2012 com. zljy. teng. js ////

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.