/** * Verification Time * @param datavalue format: YYYY-MM-DD * @returns Match returns True mismatch returns false */function ValiDate (dateValue) {var result = Da Tevalue.match (((1[8-9]\d{2}) | ( [2-9]\d{3})) (-) (10|12|0?[ 13578]) (-) (3[01]|[ 12][0-9]|0? [1-9]) $)| (^ (1[8-9]\d{2}) | ([2-9]\d{3})) (-) (11|0?) [469]) (-) (30| [12] [0-9]|0? [1-9]) $)| (^ (1[8-9]\d{2}) | ([2-9]\d{3})) (-) (0?2) (-) (2[0-8]|1[0-9]|0?) [1-9]) $)| (^ ([2468][048]00) (-) (0?2) (-) (29) $) | (^ ([3579][26]00) (-) (0?2) (-) (29) $) | (^ ([1][89][0][48]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][0][48]) (-) (0?2) (-) (29) $) | (^ ([1][89][2468][048]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][2468][048]) (-) (0?2) (-) (29) $) | (^ ([1][89][13579][26]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][13579][26]) (-) (0?2) (-) (29) $)) /); if (Result==null) {return false;} return true;} /** * Verify Phone number * @param phonevalue to verify the phone number * @returns match returns True mismatch returns false */function Validatephone (phonevalue) {var reg =/ ^[1][0-9]{10}$/;return reg.test (phonevalue);} /** * Verify Mailbox * @param emailvalue to verify the mailbox * @returns match returns True mismatch returns false */function Validateemail (emailvalue) {var reg =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/;return reg.test (emailvalue);} /** * Determine if it is a number * @param numbervalue to verify the data * @returns match returns true mismatch return False */function Isnumber (numbervalue) {//define regular expression section V Ar reg1 =/^[0-9]{0,}$/;var reg2 =/^[1-9]{1}[0-9]{0,}$/;//alert (Numbervalue); if (Numbervalue ==null | | Numbervalue.length==0) {return false;} Determine if the number is only 1 bits if (numbervalue.length<2) {return reg1.test (numbervalue);} Return Reg2.test (Numbervalue);;} /*** * Amount * @param value * @returns */function Ismoney (value) {var reg =/^[0-9]*\.? [0-9] {0,2}$/;if (Isnumber (value)) {return true;} if (value.length>3) {if (value.substr (0, 1) = = "0") {if (Value.substr (3,value.length). length>2) {return false;}}} return reg.test (value);} /** * Verify whether it is a floating-point number * @param floatvalue the data to validate * @returns match returns True mismatch returns false */function Ismyfloat (floatvalue) {var reg =/^ (\d+ ) (\.\d+) $/;if (Isnumber (Floatvalue)) {return true;} if (floatvalue.length>3) {if (floatvalue.substr (0, 1) = = "0") {if (Floatvalue.substr (0, 2)! = "0.") {return false;} }}return Reg.Test (floatvalue);} /** * Determine if it is kanji * @param charvalue to verify the data * @returns match returns True mismatch returns false */function Ischaracter (charvalue) {var reg =/^[\u4e 00-\u9fa5]{0,}$/;return reg.test (charvalue);} /** * Verify the landline number * @param telvalue to verify the landline number * @returns match returns True mismatch returns false */function Validatetel (telvalue) {var reg =/^ (\ (\d{3, 4}\) |\d{3,4}-) \d{7,8}$/;if (!reg.test (Telvalue)) {return false; }return true;} var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; Weighted factor var validecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; ID verification bit value. 10 for x/** * Authentication ID * @param idcard required to verify the ID number * @returns match returns True mismatch returns false */function Idcardvalidate (idcardvalue {//Remove the string Kinsoku Idcardvalue = Valuetrim (idcardvalue.replace (//g, "")); if (idcardvalue.length = = 15) {//perform 15-digit ID verification return Isvaliditybrithby15idcard (Idcardvalue); } else if (idcardvalue.length = = 18) {//Get ID array var a_idcard = Idcardvalue.split (""); Basic verification and 18th bit verification for 18-bit ID cardsif (Isvaliditybrithby18idcard (idcardvalue) &&istruevalidatecodeby18idcard (A_idcard)) {return true; }else {return false; }} else {return false; }}/** * Determine if the last verification bit is correct when the ID number is 18 digits * @param a_idcard ID Number Array * @return */function Istruevalidatecodeby18idcard (A_idcard) {var sum = 0;//Declare weighted sum variable if (a_idcard[17].tolowercase () = = ' x ') {a_idcard[17] = 10;//The last digit of x is the verification code Change to 10 to facilitate subsequent operation} for (var i = 0; i < i++) {sum + = wi[i] * a_idcard[i];//weighted sum} val codeposition = sum% 11; Get the Verification code location if (a_idcard[17] = = Validecode[valcodeposition]) {return true; } else {return false; }}/** * Verify that birthdays in 18-digit ID numbers are valid birthdays * @param idcard 18-bit book ID string * @return */function ISVALIDITYBRITHBY18IDCA RD (IDCARD18) {var year = idcard18.substring (6,10); var month = idcard18.substring (10,12); var day = IdCard18.substring (12,14); var temp_date = new Date (year,parsefloat (month) -1,parsefloat (day)); Get the year here with getFullYear () to avoid the millennium bug problem if (Temp_date.getfullyear ()!=parsefloat (year) | | Temp_date.getmonth ()!=parsefloat (month)-1 | | Temp_date.getdate ()!=parsefloat (day)) {return false; }else{return true; }}/** * Verify that birthdays in 15-digit ID numbers are valid birthdays * @param idCard15 15-bit book ID string * @return */function Isvaliditybrithby15id Card (IDCARD15) {var year = idcard15.substring (6,8); var month = idcard15.substring (8,10); var day = idcard15.substring (10,12); var temp_date = new Date (year,parsefloat (month) -1,parsefloat (day)); For the old ID, you do not need to consider the millennium bug problem and use the GetYear () method if (Temp_date.getyear ()!=parsefloat (year) | | Temp_date.getmonth ()!=parsefloat (month)-1 | | Temp_date.getdate ()!=parsefloat (day)) {return false; }else{return true; }}//Remove string Kinsoku function ValuetRim (str) {return str.replace (/(^\s*) | ( \s*$)/g, ""); }/** * Check 18-digit ID number (15-digit number can only detect if the birthday is correct, self-resolution) * @param idcardvalue 18-digit ID number * @returns match returns True mismatch returns false */function Idcardvi Ldate (CID) {var arrexp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//weighted factor var arrvalid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//check code var reg =/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/; if (Reg.test (CID)) {var sum = 0, idx; for (var i = 0; i < cid.length-1; i++) {//to the first 17 digits and the weight value product summation sum + = parseint (Cid.substr (i, 1), 10) * Arrexp[i]; }//calculation modulus (fixed algorithm) idx = sum% 11; Test 18th for parity with check code return ARRVALID[IDX] = = CID.SUBSTR (1). toUpperCase (); }else{return false; }}
Collation of some common JS form validation