The work of the more practical JS verification and data processing of dry goods

Source: Internet
Author: User

In the development of Web projects, inevitably encountered a variety of Web page data processing, such as the user entered in the form of the phone number, mailbox, amount, ID number, password length and complexity of the verification, and back to the background

The formatting of the data, such as the amount, returns a value of NULL, and a calculation for a day or a January before or after the specified date.

When it comes to validating data and forms, I'm sure that everyone, like me, likes to find relevant methods on the Internet because it's time-consuming to write. Today, I would like to share with you some of the common things that I have summed up in my work

Of JS.

/** * Verify password complexity (must contain number letters) * @param str * @returns true: satisfies the rule, false: does not satisfy */function validatestr (str) {var reg1 =/^ (([0-9]{1,}) ( [A-z] {1,})) | ([A-z]{1,}) ([0-9]{1,})] $/;var REG2 =/^ ([0-9]{1,}) ([A-z]{1,})] | ([A-z]{1,}) ([0-9]{1,})] $/;//var reg3 =/^ ([a-za-z]{0,}) [0-9a-z-a-z]{0,}[~] [email protected]#$%^&*.] {0,}$/;str = Valuetrim (str);//if (Reg3.test (str)) {//return true;//}if (reg1.test (str)) {return true;} if (Reg2.test (str)) {return true;} return false;} /** * Judgment string length must be greater than 8 bits less than 20 bits, generally used for password * @param str string * @returns satisfy return true */function validatelength (str) {if (Str==null | | str== ") {return false;} str = Valuetrim (str); if (parsefloat (str.length) <8) {return false;} if (parsefloat (str.length) >20) {return false;} return true;} /** * 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]"|" [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) {Phonevalue = Valuetrim (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;} Numbervalue = Valuetrim (numbervalue);//Judge 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) {if (value== ') {return false;} Value = Valuetrim (value); value = Value.replace (/(^\s*) | ( \s*$)/g, ""); 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);} /*** * Determines whether the number is between 0 and 100 * @param value * @returns */function iszerotoone (value) {if (value== ') {return false;} Value = Valuetrim (value), if (ismyfloat (value)) {if (parsefloat (value) <100 && parsefloat (value) >0) { return true;}} return false;} /** * Verify whether it is a floating-point number * @param floatvalue the data to validate * @returns match returns True mismatch returns false */function Ismyfloat (floatvalue) {if (floatvalue== ' ') {return false;} Floatvalue = ValuetriM (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}$/;telvalue = Valuetrim (Telvalue), 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 authentication for 18-bit IDs and 18th-bit authentication if (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 Isvaliditybrithby18idcard (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; }}/** * Gets the day before or after the specified date * * @param daycount * Positive for later time, negative for previous time as: 1 for Tomorrow,-1 for Yesterday * */function getdatestr (dates, D Aycount) {var dateTime = Daycount * $ * * 1000;var DD = new Date (), if (dates = = "") {dd = new date ();} else {dd = new Date (dates);} var datenumber = dd.gettime () + Datetime;var newdate = new Date (datenumber), var y = newdate.getfullyear (), var m = newdate. GetMonth () + 1;//Gets the date of the current month var d = newdate.getdate (); if (M <) {m = "0" + M;} if (d <) {d = "0" + D;} Return y + "-" + M + "-" + D;} /** * Gets the month before or after the specified month * * @param daycount * Positive number for the following month, negative for the previous month such as: 1 for the next month, 1 for the last month * */function getmonthstr (dates, Monthcount) {var dd = new Date (), if (dates = = "") {dd = new date ();} else {dd = new Date (dates);} var y = dd.getfullyear () var m = dd.getmonth () + 1;//Gets the date of the current month m = m + monthcount;if (m = = 0) {m = "n"; y = y-1;} else I F (M <) {m = "0" + m;} else if (M > N) {m = M-12;m = "0" + m;y = y + 1;} Return y + "-" + M;} /**** to Val value for UNDefined returns "", otherwise returns the original value */function Dealnull (val) {if (typeof (val) = = "undefined") {return "";} else {return val;}}
Note: Some codes refer to the online information.

The work of the more practical JS verification and data processing of dry goods

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.