JS form verification method instance summary [telephone, ID number, email, Chinese, special characters, ID number, etc.]

This example summarizes the JS form verification method. Share to you for your reference, the specific use:

Back to write form, return to write different check JS, very trouble, then write universal check functions, very rough, but more practical, then change again Change:

Contains page: check-form.js

The code is as follows:

// rule check Sort Function Regheck (OBJS) {var str = objs.checktype; switch (str) {case “cn”: // The input type of the form control to check must be Chinese Return CNWORDREGCHECK (OBJS); Break; Case “IDnum”: Return IdcardRegCheck (OBJS); // The input type of the form control to be checked must be the ID number Break; Case “NUM”: // The input type of the form control to check must be a digital return NumRegcheck (Objs); Break; Case “Mail”: // The input type of the form control to be checked must be Email Return EmailRegcheck (Objs); Break; Case “txt”: // The input type of the form control to check must be a string Return SpecialwordRegcheck (Objs); Break; Case “Notes” : Return True; // The input type of the form control to check must be Break;}} // *********************************** *********************// Check the phone number Function NumRegcheck (obj) {var uplimit = obj.checkrule.split (“,”) [0]; var downlimit = obj.checkrule.split (“,”) [1]; var rag = “” ; if (Download == NULL) {reg = evAl_r (“/ ^ [0-9] {” + UPLIMIT + “} $ /”);} else {reg = EVAL_R (“/ ^ [0-9] {” + UPLIMIT + “,” + DONLIMIT + “} $ /”);} var str = obj.value; var flag = reg.test (str); return flag;} // ************ *********************************** / / Check the ID number function IDCARDREGCHECK (OBJ) {var str = Obj.value; var rag = / ^ ([0-9] {15} | [0-9] {18}) $ /; var flag = reg.test (str); return flag;} // *** ***************************************************** / / Check EmailFunction EmailReck (obj) {var str = obj.value; var rag = /^([A-ZA-Z0-9_-] )@([A-ZA-Z0-9_-] )+(( \.[a- ZA-Z0-9_-] {2,3}) {1, 2}) $ /; var flag = reg.test (str); return flag;} // ************ *************************** / / Check Chinese input function cnwordRegcheck (obj) {var str = obj.value; var rag = / ^ [\ U4E00- \ u9fa5] (\ s * [\ u9fa5]) * $ /; var flag = reg.test (str); // alert (flag); return flag;} // ******** ******************************** / / Check special characters Function SpecialwordRegCheck (obj) {var rag = / [\ / / ()] / g “> \\) (“) (
)] / g; var str = obj.value; var flag = reg.test (str); flag =! Flag Return flag;} // ************************************************** ***** // Check the main boot function function checkform (obj) {var myform = Eval_r ("Document." + Obj.name); for (i = 0; i  myform.eferences [i] .maxlength )) {Alert ("You forgot to fill in" + myform.ersion! "+" or you fill in the information! "); MyForm.ersion [i] .focus (); return false Break;} if (MyForm.eferences [i] .value == 0) {Alert ("You forgot to Select" + MyForm.elements [i] .cnname + "!"); myform.eferences [i] .focus (); return false; break;} // data standardization check var myobj = myform.Elements [i]; //alert(MYOBJ.CHECKTYPE); / / Break; if (! regcheck (myobj)) {alert (myobj.cnname + "is incorrect, please fill in the requirements!"); myobj.focus (); return false; break;}}}     Attached: JS ID number strict verification 

Function checkidcard (IDCARD) {var errors = new array (“Verification Pass! “,” ID card number bit is not right! “,” ID number birth date is exceeding range or contain illegal characters! “,” ID card number checksion! “,” Identity card area illegal! “); Var area = {11: “Beijing”, 12: “Tianjin”, 13: “Hebei”, 14: “Shanxi”, 15: “Inner Mongolia”, 21: “Liaoning”, 22: “Jilin”, 23: “Heilongjiang”, 31: “Shanghai”, 32: “Zhejiang”, 33: “Anhui”, 35: “Fujian”, 36: “Jiangxi”, 37: “Shandong”, 41: “Henan”, 42: “Hubei”, 43: “Hunan”, 44: “Guangdong”, 45: “Guangxi”, 46: “Hainan”, 50: “Chongqing”, 51: “Sichuan”, 52: “Guizhou”, 53: “Yunnan” “, 54:” Tibet “, 61:” Shaanxi “, 62:” Gansu “, 63:” Qinghai “, 64:” Ningxia “, 65:” Xinjiang “, 71:” Taiwan “, 81:” Hong Kong “, 82: “Macau”, 91: “Foreign”} Var IDCard, Y, JYM; Var S, M; VAR IDCARD_ARRAY = New Array (); IDCARD_ARRAY = IDCARD.SPLIT (“”); // Regional Test IF (Idcard.Substr (0, 2))] == NULL) Return Errors [4]; // identity number bits and formats Switch (idcard.length) {case 15: IF ((Idcard.Substr (6, 2)) + 1900)% 4 == 0 || (PARSEINT (IDCard.substr (6, 2)) + 1900)% 100 == 0 && (PARSEINT (IDCard.substr (6,2)) + 1900)% 4 == 0)) {EREG = / ^ [1 -9] [0-9] {5} [0-9] {2} ((01 | 03 | 05 | 07 | 08 | 10 | 12) (0 [1-9] | [1-2] [0 -9] | 3 [0-1]) | (04 | 06 | 09 | 11) (0 [1-9] | [1-2] [0-9] | 30) | 02 (0 [1-9 ] | [1-2] [0-9])) [0-9] {3} $ /; // Test the legitimacy of the date of birth} else {EREG = / ^ [1-9] [0-9] {5} [0-9] {2} ((01 | 03 | 05 | 07 | 08 | 10 | 12) (0 [1-9] | [1-2] [0-9] | 3 [0- 1]) | (04 | 06 | 09 | 11) (0 [1-9] | [1-2] [0-9] | 30) | 02 (0 [1-9] | 1 [0-9] | 2 [0-8])) [0-9] {3} $ /; // Test the legitimacy of the date of birth} if (EREG.Test (IDCARD)) Return Errors [0]; Else Return Errors [2] Break; Case 18: // 18 identity number detection // born date legitimacy check // Leap year monthly: ((01 | 03 | 05 | 07 | 08 | 10 | 12) (0 [1-9] | [1-2] [0-9] | 3 [0-1]) | (04 | 06 | 09 | 11) (0 [1-9] | [1-2] [0-9] | 30) | 02 (0 [1-9] | [1-2] [0-9])) // White Month: ((01 | 03 | 05 | 07 | 08 | 10 | 12) (0 [1-9] | [1-2] [0-9] | 3 [0-1]) | (04 | 06 | 09 | 11) 0 [1-9] | [1-2] [0-9] | 30) | 02 (0 [1-9] | 1 [0-9] | 2 [0-8])) IF (PARSEINT (Idcard) .SUBSTR (6, 4))% 4 == 0 || (PARSEINT (IDCard.Substr (6,4))% 100 == 0 && Parseint (IDCard.substr (6,4))% 4 == 0) ) {EREG = / ^ [1-9] [0-9] {5} 19 [0-9] {2} ((01 | 03 | 05 | 07 | 08 | 10 | 12) (0 [1-9 ] | [1-2] [0-9] | 3 [0-1]) | (04 | 06 | 09 | 11) (0 [1-9] | [1-2] [0-9] | 30 ) | 02 (0 [1-9] | [1-2] [0-9] {3} [0-9xx] $ /; // Legitizes the legitimacy of the date of birth } else {EREG = / ^ [1-9] [0-9] {5} 19 [0-9] {2} ((01 | 03 | 05 | 07 | 08 | 10 | 12) (0 [1- 9] | [1-2] [0-9] | 3 [0-1]) | (04 | 06 | 09 | 11) (0 [1-9] | [1-2] [0-9] | 30) | 02 (0 [1-9] | 1 [0-9] | 2 [0-9])) [0-9] {3} [0-9xx] $ /; // Legal date of birthday date Regular regular expression} if (EREG.TEST (IDCARD)) {// Test the legality of the date of birth // Calculate the check bit S = (PARSEINT (IDCARD_ARRAY [0]) + PARSEINT (IDCARD_ARRAY [10]) * 7+ (IDCARD_ARRAY [1]) + PARSEINT (IDCARD_ARRAY [11]) * 9+ (Parseint (IDCARD_ARRAY [2]) + PARSEINT (IDCARD_ARRAY [12])) * 10+ ( PARSEINT (IDCARD_ARRAY [3]) + PARSEINT (IDCARD_ARRAY [13])) * 5+ (Idcard_Array [4]) + PARSEINT (IDCARD_ARRAY [14])) * 8+ (Parseint (IDCARD_ARRAY [5]) + PARSEINT IDCARD_ARRAY [15])) * 4+ (IDCard_Array [6]) + PARSEINT (IDCARD_ARRAY [16])) * 2+ parseint (IDCARD_ARRAY [7]) * 1+ parseint (IDCARD_ARRAY [8]) * 6+ Parseint (IDCARD_ARRAY [9]) * 3; Y = S% 11; m = “f”; jYM = “10×98765432”; m = jym.substr (y, 1); // Judgment the check bit IF (M == IDCARD_ArRay [17]) RETURN Errors [0]; // Detect ID Error Else Return Errors [3];} else return error [2]; Break; default: return error [1]; Break;}} alert (checkidcard (“220183198808081610”))
  PS: Here you will provide you with 2 very convenient regular expression tools for everyone to use:    
JavaScript regular expression online test tool:
http: // Tools.jb51.net/regex/javascript

Regular expression online generation tool:

http://tools.jb51.net/regex/create_reg
More about JavaScript-related content, readers who are interested in this site: “JavaScript regular expression skills”, “JavaScript Replace Skill Summary”, “JavaScript Find Algorithm Skills Summary”, “JavaScript Data Structure and Algorithm Skills Summary” , “JavaScript Supreme Algorithm and Skills Summary”, “JavaScript JSON Operation Skills Summary”, “JavaScript Error and Debugging Skill Summary” and “Summary of JavaScript Mathematical Computing Method”

I hope this article will be designed to everyone JavaScript programming Help.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment