/** * 2010-7-13 * He Chen* Love* js various form data verification*//****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** Verification of digital**********************************************************************************************************************************************************************************************************************//** * Check whether the input string is all numbers* Input:str string* Return:true or flase; true is expressed as a number*/function checkNum(str){ return str.match(//D/) == null;}/** * Check whether the input string is a decimal* Input:str string* Return:true or flase; true is expressed as a decimal*/function checkDecimal(str){ if (str.match(/^-?/d+(/./d+)?$/g) == null) { return false; } else { return true; }}/** * Check whether the input string is integer data* Input:str string* Return:true or flase; true is expressed as a decimal */function checkInteger(str){ if (str.match(/^[-+]?/d*$/) == null) { return false; } else { return true; }}/*********************************************************************************//****************************************************************************************************************** *************************************************************************************************************************************************************************************************************************** * Check whether the input string is a character* Input:str string* Return:true or flase; true means that all characters do not contain Chinese characters*/function checkStr(str){ if (/[^/x00-/xff]/g.test(str)) { return false; } else { return true; }}/** * Check whether the input string contains Chinese characters* Input:str string* Return:true or flase; true means that Chinese characters*/function checkChinese(str){ if (escape(str).indexOf("%u") != -1) { return true; } else { return false; }}/** * Check whether the entered mailbox format is correct* Enter:str string* Return:true or flase; true means the format is correct*/function checkEmail(str){ if (str.match(/[A-Za-z0-9_-]+[@](/S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(/S*)/g) == null) { return false; } else { return true; }}/** * Check whether the entered mobile phone number is correct* Enter:str string* Return:true or flase; true means the format is correct*/function checkMobilePhone(str){ if (str.match(/^(?:13/d|15[89])-?/d{5}(/d{3}|/*{3})$/) == null) { return false; } else { return true; }}/** * Check whether the entered landline number is correct* Enter:str string* Return:true or flase; true means the format is correct*/function checkTelephone(str){ if (str.match(/^(([0/+]/d{2,3}-)?(0/d{2,3})-)(/d{7,8})(-(/d{3,}))?$/) == null) { return false; } else { return true; }}/** * Check whether the format of QQ is correct* Enter:str string* Return:true or flase; true means the format is correct*/function checkQQ(str){ if (str.match(/^/d{5,10}$/) == null) { return false; } else { return true; }}/** * Check whether the entered ID number is correct* Enter:str string* Return:true or flase; true means the format is correct*/function checkCard(str){ //15-digit ID card regular expression var arg1 = /^[1-9]/d{7}(0/d)|(1[0-2]))(([0|1|2]/d)|3[0-1])/d{3}$/; //18-digit ID card regular expression var arg2 = /^[1-9]/d{5}[1-9]/d{3}(0/d)|(1[0-2]))(([0|1|2]/d)|3[0-1])((/d{4})|/d{3}[AZ])$/; if (str.match(arg1) == null && str.match(arg2) == null) { return false; } else { return true; }}/** * Check whether the entered IP address is correct* Enter:str string * Return: true or flase; true means correct format*/function checkIP(str){ var arg = /^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$/; if (str.match(arg) == null) { return false; } else { return true; }}/** * Check whether the input URL address is correct* Input:str string* Return:true or flase; true means the format is correct*/function checkURL(str){ if (str.match(/(http[s]?|ftp):://///[^///.]+?/..+/w$/i) == null) { return false } else { return true; }}/** * Check whether the input characters have special characters* Input:str string* Return:true or flase; true means that special characters* Mainly used for verification when registering information*/function checkQuote(str){ var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")"); items.push(":", ";", "'", "|", "//", "<", ">", "?", "?", "<<", ">>", "||", "//"); items.push("admin", "administrators", "administrator", "administrator", "administrator", "system administrator"); items.push("select", "delete", "update", "insert", "create", "drop", "alter", "trancate"); str = str.toLowerCase(); for (var i = 0; i < items.length; i++) { if (str.indexOf(items[i]) >= 0) { return true; } } return false;}/************************************************************************//************************************************Time Verification of ********************************************************************************************************************************************************************************************************************** * Check whether the date format is correct* Enter:str string* Return:true or flase; true means the format is correct* Note: The Chinese date format cannot be verified here* Verify the short date (2007-06-05) */function checkDate(str){ //var value=str.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)$))/); var value = str.match(/^(/d{1,4})(-|//)(/d{1,2})/2(/d{1,2})$/); if (value == null) { return false; } else { var date = new Date(value[1], value[3] - 1, value[4]); return (date.getFullYear() == value[1] && (date.getMonth() + 1) == value[3] && date.getDate() == value[4]); }}/** * Check whether the time format is correct* Enter:str string* Return: true or flase; true means the format is correct* Verification time(10:57:10) */function checkTime(str){ var value = str.match(/^(/d{1,2})(:)?(/d{1,2})/2(/d{1,2})$/) if (value == null) { return false; } else { if (value[1] > 24 || value[3] > 60 || value[4] > 60) { return false } else { return true; } }}/** * Check whether the full date time format is correct* Enter:str string* Return:true or flase; true means the format is correct* (2007-06-05 10:57:10) */function checkFullTime(str){ //var value = str.match(/^(/d{1,4})(-|//)(/d{1,2})/2(/d{1,2}) (/d{1,2}):(/d{1,2}):(/d{1,2})$/); var value = str.match(/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/); if (value == null) { return false; } else { //var date = new Date(checkFullTime[1], checkFullTime[3] - 1, checkFullTime[4], checkFullTime[5], checkFullTime[6], checkFullTime[7]); //return (date.getFullYear() == value[1] && (date.getMonth() + 1) == value[3] && date.getDate() == value[4] && date.getHours() == value[5] && date.getMinutes() == value[6] && date.getSeconds() == value[7]); return true; } }/*********************************************************************************//****************************************************************** ID number Verification of ********************************************************************************************************************************************************************************************************** * 15-digit encoding rules for ID card: ddddddd yymmdd xx p * dddddd: Region code* yymmdd: Date of birth* xx: Sequential class encoding, cannot be determined* p: Gender, odd number is male, even number is female* <p /> * 18-digit encoding rules for ID card: dddddddd yyyymmdd xxx y * dddddd: Region code* yyyymmdd: Date of birth* xxx: Sequential class encoding, cannot be determined, odd number is male, even number is female* y: Verification code, this digit value can be obtained through the first 17 digits* <p /> * The weighting factor of the 18-digit number is (from right to left) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ] * Verification bit Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] * Verification bit calculation formula: Y_P = mod( ∑(Ai×Wi),11 ) * i is the 2...18 digits of the ID number from right to left; Y_P is the verification code array position where the foot check code is located* */var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 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 function IdCardValidate(idCard){ idCard = trim(idCard.replace(/ /g, "")); if (idCard.length == 15) { return isValidityBrithBy15IdCard(idCard); } else if (idCard.length == 18) { var a_idCard = idCard.split("");// Get the ID card array if (isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) { return true; } else { return false; } } else { return false; }}/** * Determine whether 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 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 convenient subsequent operations} for (var i = 0; i < 17; i++) { sum += Wi[i] * a_idCard[i];// Weighted sum} valCodePosition = sum % 11;// Where to get the verification code if (a_idCard[17] == ValideCode[valCodePosition]) { return true; } else { return false; }}/** * Determine whether it is a man or a woman by ID card* @param idCard 15/18-digit ID card number* @return 'female'-female, 'male'-male*/function maleOrFemalByIdCard(idCard){ idCard = trim(idCard.replace(/ /g, ""));// Process the ID number. Including spaces between characters. if (idCard.length == 15) { if (idCard.substring(14, 15) % 2 == 0) { return 'female'; } else { return 'male'; } } else if (idCard.length == 18) { if (idCard.substring(14, 17) % 2 == 0) { return 'female'; } else { return 'male'; } } else { return null; }}/** * Verify that the birthday in the 18-digit ID 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 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 whether the birthday in the 15-digit ID card number is a valid birthday* @param idCard15 15-digit book ID card 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 trim(str){ return str.replace(/(^/s*)|(/s*$)/g, "");}The above is the entire content of this article. For more information about JavaScript, you can check out: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.