Preface
When developing web projects, it is inevitable to encounter various processing of web page data, such as verification of the phone number, email address, amount, ID number, password length and complexity entered by the user in the form, and returning to the background
Format of data such as amount, the returned value is null, and there is also calculation of a certain day or month before or after the specified date.
Below are some of the commonly used js in your work that you have summarized. If you need it, you can refer to it and learn it.
/** * Verify password complexity (must contain numeric letters) * @param str * @returns true:Satisfies the rule, false: Not satisfied */ function validateStr(str){ var reg1 = /^(([0-9]{1,})([az]{1,}))|(([az]{1,})([0-9]{1,}))$/; var reg2 = /^(([0-9]{1,})([AZ]{1,}))|(([AZ]{1,})([0-9]{1,}))$/; //var reg3 = /^([a-zA-Z]{0,})[0-9a-zAz]{0,}[~`!@#$%^&*.]{0,}$/; str = valueTrim(str); //if(reg3.test(str)){ // return true; //} if(reg1.test(str)){ return true; } if(reg2.test(str)){ return true; } return false; } /** * It is judged that the length of the string must be greater than 8 bits and less than 20 bits, which is generally used for passwords* @param str string* @returns If satisfying return true */ function valiDateLength(str){ if(str==null || str==''){ return false; } str = valueTrim(str); if(parseFloat(str.length)<8 ){ return true */ function valiDateLength(str){ if(str==null || str==''){ return false; } str = valueTrim(str); if(parseFloat(str.length)<8 ){ 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 The format is: YYYY-MM-DD * @returns The match returns true If the match does not match returns false */ function valiDate(dateValue){ var result = dateValue.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)$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([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 The phone number to be verified* @returns The match returns true If the mismatch returns false */ function validatePhone(phoneValue) { phoneValue = valueTrim(phoneValue); var reg = /^[1][0-9]{10}$/; return reg.test(phoneValue); } /** * Verify email* @param emailValue The email to be verified* @returns The match returns true If the mismatch returns false */ function validateEmail(emailValue){ var reg = /^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/; return reg.test(emailValue); } /** * Determine whether it is a number* @param numberValue The data to be verified* @returns The match returns true If the match does not match returns false */ function isNumber(numberValue){ //Define the regular expression part var 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 when the number is only 1 bit 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); } /*** * Determine whether it is a number 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 be verified* @returns The match returns true If the 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 whether it is a Chinese character* @param charValue Data to be verified* @returns match returns true If a match does not match returns false */ function isCharacter(charValue){ var reg = /^[/u4e00-/u9fa5]{0,}$/; return reg.test(charValue); } /** * Verify the landline number* @param telValue The landline number to be verified* @returns The match returns true If the 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, 6, 3, 7, 9, 10, 5, 8, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 ]; // Weighting factor var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]; // The ID card verification bit value.10 represents X /** * Verify ID card* @param idCard The ID card number that needs to be verified* @returns The match returns true If the match does not match returns false */ function IdCardValidate(idCardValue) { // Remove the string head and tail spaces idCardValue = valueTrim(idCardValue.replace(/ /g, "")); if (idCardValue.length == 15) { // Verify the 15-bit ID card return isValidityBrithBy15IdCard(idCardValue); } else if (idCardValue.length == 18) { // Get the ID card array var a_idCard = idCardValue.split(""); // Perform basic verification of 18-bit ID card and 18-bit verification if(isValidityBrithBy18IdCard(idCardValue)&&isTrueValidateCodeBy18IdCard(a_idCard)){ return true; }else { return false; } } else { return false; } } /** * Determine whether the last verification bit is correct when the ID card number is 18-bit* @param a_idCard ID number array* @return */ function isTrueValidateCodeBy18IdCard(a_idCard) { var sum = 0; // Declare the weighted sum variable if (a_idCard[17].toLowerCase() == 'x') { a_idCard[17] = 10;// Replace the verification code with the last bit x with 10 for subsequent operations} for ( var i = 0; i < 17; i++) { sum += Wi[i] * a_idCard[i];// Weighted sum} valCodePosition = sum % 11; // The position of the verification code obtained if (a_idCard[17] == ValideCode[valCodePosition]) { return true; } else { return false; } } /** * Verify whether the birthday in the 18-digit ID card number is a valid birthday* @param idCard 18-digit 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)); // Use getFullYear() here to get the year to avoid the problem of millennium bug if(temp_date.getFullYear()!=parseFloat(year) ||temp_date.getMonth()!=parseFloat(month)-1 ||temp_date.getDate()!=parseFloat(day)){ return false; }else{ return true; } } /** * Verify whether the birthday in the 15-digit ID card number is a valid birthday* @param idCard15 15-digit book ID string* @return */ function isValidityBrithBy15IdCard(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 your age in your old ID card, you don't need to consider the millennium bug issue 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 the string head and tail spaces function valueTrim(str) { return str.replace(/(^/s*)|(/s*$)/g, ""); } /** * Check the 18-digit ID number (15-digit number can only detect whether the birthday is correct, and solve it yourself) * @param idCardValue 18-digit ID number* @returns Match returns true If the mismatch returns false */ function idCardVildate(cid){ var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];// Weighting 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++){ // Sum sum of the first 17 digits and weights += parseInt(cid.substr(i, 1), 10) * arrExp[i]; } // Calculate the modulus (fixed algorithm) idx = sum % 11; // Check whether the 18th is equal to the check code return arrValid[idx] == cid.substr(17, 1).toUpperCase(); }else{ return false; } } /** * Get the day before or after the specified date* * @param dayCount * Positive number is the future time, negative number is the previous time, such as: 1 is expressed as tomorrow, -1 is yesterday* */ function getDateStr(dates, dayCount) { var dateTime = dayCount * 24 * 60 * 60 * 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;// Get the date of the current month var d = newDate.getDate(); if (m < 10) { m = "0" + m; } if (d < 10) { d = "0" + d; } return y + "-" + m + "-" + d; } /** * Get the month before or after the specified month* * @param dayCount * Positive number is the next month, negative number is the previous month, such as: 1 is the next month, -1 is the previous 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;// Get the date of the current month m = m + monthCount; if (m == 0) { m = "12"; y = y - 1; } else if (m < 10) { m = "0" + m; } else if (m > 12) { m = m - 12; m = "0" + m; y = y + 1; } return y + "-" + m; } /** * Return "" for the val value is undefined, otherwise return the original value*/ function dealNull(val) { if (typeof (val) == "undefined") { return ""; } else { return val; } }Summarize
The above are the more practical JS verification and data processing information compiled for everyone, which is very helpful for everyone's daily work. It is recommended that you collect it for easy review in the future. Thank you for your support for Wulin.com.