Home >Web Front-end >JS Tutorial >Page JavaScript validation function

Page JavaScript validation function

高洛峰
高洛峰Original
2016-11-25 13:22:22921browse

/**************************************************************************************/
/***************************************Verification of numbers************ *******************************/
/**************************************************************************************/
/**
* Check whether the input string of characters are all numbers
* Input: str string
* Return: true or false; true is expressed as a number
*/
function checkNum(str){
return str.match(/D/) == null;
}

/**
* Check whether the input string of characters is a decimal
* Input: str string
* Return: true or flase; true represents a decimal
*/
function checkDecimal(str){
if (str.match(/^-?d+(.d+)?$/g) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the input string of characters 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;
}
}
/**************************************************************************************/
/***************************************Verification of characters************ *****************************/
/**************************************************************************************/

/**
* Check whether the entered string of characters is a character
* Input: str string
* Return: true or flase; true means all characters are characters and does not contain Chinese characters
*/
function checkStr(str){
if (/[^x00-xff]/g.test(str)) {
return false;
}
else {
return true;
}
}

/**
* Check whether the input string of characters contains Chinese characters
* Input: str string
* Return: true or false; true means it contains Chinese characters
*/
function checkChinese(str){
if (escape(str).indexOf("%u") != -1) {
return true;
}
else {
return false;
}
}

/**
* Check whether the entered email format is correct
* Input: 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 format of the entered mobile phone number is correct
* Input: str string
* Return: true or false; true means the format is correct
*/
function checkMobilePhone(str){
if (str.match(/^(?:13d|15[89])-?d{5}(d{3}|*{3})$/) == null) {
return false;
}
else {
return true;
}
}

/**
* Check whether the entered fixed phone number is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkTelephone(str){
if (str.match(/^(([0+]d{2,3}-)?(0d{2,3})-) (d{7,8})(-(d{3,}))?$/) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the format of QQ is correct
* Input: 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
* Input: 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}((0d)|(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}((0d)|(1[0-2]))(([0|1|2]d)|3[0-1]) ((d{4})|d{3}[A-Z])$/;
if (str.match(arg1) == null && str.match(arg2) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the entered IP address is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkIP(str){
var arg = /^(d{1,2}|1dd|2[0-4]d| 25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4] d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/;
if (str.match(arg) == null ) {
return false;
}
else {
return true;
}
}
/**
* Check whether the entered 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 entered characters have special characters
* Input: str string
* Return: true or flase; true means it contains special characters
* Mainly used for verification when registering information
*/
function checkQuote(str){
var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
items.push(":", ";", "'", "|", "\", "<", ">", "?", "/", "<<", ">>", "||", "//");
items. push("admin", "administrators", "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************ *******************************/
/**************************************************************************************/
/**
* Check whether the date format is correct
* Input: str string
* Return: true or flase; true means the format is correct
* Note: Chinese date format cannot be verified here
* Verify 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)$)|(^([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
* Input: str string
* Return: true or flase; true means the format is correct
* Verify 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 and time format is correct
* Input: 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]))-(?:(?:[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;
}

}
 

/**************************************************************************************/
/**************************************Verification of ID number************ **************************/
/**************************************************************************************/
/**
* 15-digit coding rules for ID cards: dddddd yymmdd xx p
* dddddd: area code
* yymmdd: date of birth
* xx: sequential coding, cannot be determined
* p: gender, odd numbers are male, even numbers are female
*


* ID card 18-digit coding rules: dddddd yyyymmdd xxx y
* dddddd: area code
* yyyymmdd: date of birth
* xxx: sequential coding, cannot be determined, odd numbers are male, Even numbers are female
* y: Check code, the value of this digit can be obtained by calculating the first 17 digits
*


* 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 ]
* Check digit calculation formula: Y_P = mod( ∑(Ai×Wi),11 )
* i is 2...18 of the ID card number counting from right to left bit; Y_P is the position of the check code array 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];// 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 ( IsvalidityBRIDHBY18idCard (IDCard) && iStrueValidateCodeby18idcard (A_IDCARD) {
Return True; AReturn false;
}}}
/**
* Determine whether the last verification digit is correct when the ID number is 18 digits
* @param a_idCard ID card 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; // Change the last bit to Replace the verification code of valCodePosition = sum % 11; // Get the verification code position
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
}
else {
return false;
}
}
/**
* Determine whether you are male or female by ID card
* @param idCard 15/18-digit ID number
* @return 'female'-female, 'male'-male
*/
function maleOrFemalByIdCard(idCard){
idCard = trim(idCard.replace(/ /g, ""));// Process the ID number. Include 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 whether the birthday in the 18-digit ID card number is a valid birthday
* @param idCard 18-digit ID card 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 Y2K 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 number is a valid birthday
* @param idCard15 15-digit 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 old Your age in the ID card does not need to consider the Y2K 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 leading and trailing spaces from the string
function trim(str){
return str.replace(/(^s*)|(s*$)/g, "");
}



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn