This article mainly introduces various javascript verification rules, involving javascript Detection Techniques for mobile phone numbers, email addresses, URLs, Chinese characters, and images, which are of reference value, for more information about how to implement javascript verification, see the following examples. Share it with you for your reference. The details are as follows:
/*** Check various rules ** @ param str check content * @ param cType pre-configured check rule string [* empty, check whether it is blank * telphone, landline phone number * allphone, all mobile phone numbers * ydphone, mobile phone number * ltphone, Unicom mobile phone number * dxphone, Telecom Mobile Phone Number * email, email * url, url * cn, Chinese character * image, the image format is * emscode, zip code * isEmpty. check whether it is empty * isint, integer * isfloat, whether it is a positive decimal * isnumber, and whether it is a real number * words, determine whether it is an English letter * wordsAndNum, whether it is a letter + number * wordsAndNumAndDownline, and whether it is a string * qq consisting of digits, 26 English letters, or underscores, QQ test * personCard18, ID card 18 digits * personCard15, ID card 15 digits *] * @ param regex custom expression input format for example: "^ \-? [1-9] + \ d * $ "** @ description cType and regex can only be empty. * For example, checkObjectByRegex (" test Chinese "," cn "); // judge Chinese characters * such as checkObjectByRegex ("test Chinese", null, "^ [\ u4e00-\ u9fa5] + $ "); // custom expression regular * @ return {boolean} */function checkObjectByRegex (str, cType, regex) {/*** defines the regular expression object for verifying various formats */var Regexs = {telphone: (/^ (\ d {3 }\)) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} $/), // landline phone number allphone: (/^ (13 [0-9]) | (14 [57]) | (15 [0-9]) | (17 [678]) | (18 [0-9]) [0-9] {8} $ /), // all mobile phone numbers: (/^ (13 [4-9]) | (15 [012789]) | 147 | 178 | (18 [23478]) [0-9] {8 }$/), // mobile phone number ltphone: (/^ (13 [0-2]) | (145) | (15 [56]) | (176) | (18 [56]) [0-9] {8 }$/), // China Unicom mobile phone number dxphone: (/^ (133) | (153) | (177) | (180) | (181) | (189) [0-9] {8} $ /), // Telecom phone number email: (/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((\. [a-zA-Z0-9 _-] {2, 3 }) {1, 2}) $/), // email url :(/(?: Https | http | ftp | rtsp | mms ):\/\/. + \/[\ w] + \. [\ w] +/), // URL cn: (/^ [\ u4e00-\ u9fa5] + $/I), // Chinese character image :(/\. jpg $ | \. jpeg $ | \. png $/I), // emscode: (/^ [1-9] \ d {5} $/), // zip code isint: (/^ (\-)? [1-9] + \ d * $/), // integer isfloat: (/^ [0-9] + \.? [0-9] * $/), // determines whether it is a positive decimal isnumber: (/^ [-\ +]? \ D + (\. \ d + )? $/), // Judge as A real number of words: (/^ [A-Za-z] + $/), // determine whether it is an English letter wordsAndNum: (/^ [A-Za-z0-9] + $/), // determines whether it is a letter + number wordsAndNumAndDownline: (/^ \ w + $ /), // determine whether a string consisting of digits, 26 English letters, or underscores (_) qq: (/^ [1-9] \ d {} $/), // QQ personCard18: (/^ (\ d {6 })()? (\ D {4}) (\ d {2}) (\ d {2}) (\ d {3}) (\ d | X) $ /), // ID card 18-digit personCard15: (/^ (\ d {6 })()? (\ D {2}) (\ d {2}) (\ d {2}) (\ d {3}) $/) // ID card 15 digits }; var nReg; if (str = null | typeof (str) = "undefined") {str = "";} if (cType! = Null & typeof (cType )! = "Undefined") {if (cType = "isEmpty") {str = $. trim (str); if (str! = Null & typeof (str )! = "Undefined" & str! = "") {Return false;} else return true;} nReg = Regexs [cType]; if (str = null | str = "") return false; // The input is null and is considered as verified. // if (cType = 'personcard18') {var ary = str for an 18-digit ID card. match (Regexs [cType]); if (! (ParseInt (ary [3])> = 1900) return false; var D = new Date (ary [3] + "/" + ary [4] + "/" + ary [5]); var isTrue = D. getFullYear () = ary [3] & (D. getMonth () + 1) = ary [4] & D. getDate () = ary [5]; return isTrue;} // if (cType = 'personcard15') {var ary = str. match (Regexs [cType]); var D = new Date ("19" + ary [3] + "/" + ary [4] + "/" + ary [5]); var isTrue = D. getYear () = ary [3] & (D. g EtMonth () + 1) = ary [4] & D. getDate () = ary [5]; return isTrue ;}} else {// custom Regular Expression Processing if (regex! = Null & typeof (regex )! = "Undefined") {nReg = new RegExp (regex) ;}else {return false ;}} return nReg. test (str );}
I hope this article will help you design javascript programs.