In this article, I have written 12 super useful regular expressions, which are the favorites of WEB developers.
1. You can only enter the amount in the input box, but in fact you can only enter numbers with up to two decimal places.
//The first type is restricted in the input input box <input type="text" maxlength="8" class="form-control" style="margin-right: 2px;" value="" onChange="count();" onkeyup="if (==this.value2) return; if ((/^\d*(?:\.\d{0,2})?$/)==-1) =(this.value2)?this.value2:'';else this.value2=;">Yuan //The second way to dynamically increase the form can only be verified in the js method. var amount=$("#amount").val(); if ((/^\d*(?:\.\d{0,2})?$/)==-1) { alert("The amount format is wrong, there are at most two decimal places"); return false; }
2. Verify the email format
var reg=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; var email=$("#email").val(); if(!(email) ){ alert("Please enter an email account that meets the specifications!"); return false; }
3. Passwords are made of numbers, letters, special characters and the length is 8-20 digits.
function validatePwd(str) { if (/^.*?[\d]+.*$/.test(str) && /^.*?[A-Za-z]/.test(str) && /^.*?[~/`!@#$%^&*()_+|{}?;:><\-\]\\[\/].*$/.test(str) && /^.{8,20}$/.test(str)) { return true; } return false; }
4. Verify the phone number
/** * Verify phone number * @param phoneValue Phone Number to be verified * @returns match returns true If not match returns false */ function validatePhone(phoneValue) { phoneValue = valueTrim(phoneValue); var reg = /^[1][0-9]{10}$/; return (phoneValue); }
5. Determine whether it is Chinese characters
/** * Determine whether it is Chinese characters * * @param charValue * Data to be verified * @returns match returns true If not match returns false */ function isCharacter(charValue) { var reg = /^[\u4e00-\u9fa5]{0,}$/; return (charValue); }
6. Whether it is a letter: true: Yes, false: No
function isChar(charValue){ var charPattern=/^[a-zA-Z]*$/; //Is it a letter? result=(charValue); return result; }
7. Determine whether it is a number
function isNum(numValue){ var numPattern=/^\d*$/; // Regular expression of numbers result=(numValue); return result; }
8. Regular expressions of integers
function isInt(intValue){ var intPattern=/^0$|^[1-9]\d*$/; // Regular expression of integers result=(intValue); return result; }
9. Whether it is letters and numbers
function isCharNum(flagValue){ var flagPattern=/^[a-zA-Z0-9]*$/; //Is it letters and numbers? result=(flagValue); return result; }
10. Check the 18-digit ID number
/** * Check the 18-digit ID number (15-digit number can only check whether the birthday is correct, and solve it yourself) * * @param idCardValue * 18-digit ID number * @returns match returns true If not match 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 factorvar arrValid = [ 1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2 ];// Verification codevar 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 ((cid)) { var sum = 0, idx; for (var i = 0; i < - 1; i++) { // Sum the product of the first 17 digits and the weightssum += parseInt((i, 1), 10) * arrExp[i]; } // Computational modulus (fixed algorithm)idx = sum % 11; // Check whether the 18th is equal to the verification codereturn arrValid[idx] == (17, 1).toUpperCase(); } else { return false; } }
11. Verify whether the birthday in the 15-digit ID number is a valid birthday
function isValidityBrithBy15IdCard(idCard15) { var year = (6, 8); var month = (8, 10); var day = (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 problem of Millennium Worm 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; } }
12. Verify whether the birthday in the 18-digit ID number is a valid birthday
function isValidityBrithBy18IdCard(idCard18) { var year = (6, 10); var month = (10, 12); var day = (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 bugs if (temp_date.getFullYear() != parseFloat(year) || temp_date.getMonth() != parseFloat(month) - 1 || temp_date.getDate() != parseFloat(day)) { return false; } else { return true; } }