Home > Web Front-end > JS Tutorial > Common regular rules for javascript form validation_javascript skills

Common regular rules for javascript form validation_javascript skills

WBOY
Release: 2016-05-16 18:45:36
Original
1039 people have browsed it
Copy code The code is as follows:

/*
Purpose: Verify the format of the ip address
Input: strIP: ip address
Return: true if the verification is passed, otherwise false;
*/
function isIP(strIP) {
if (isNull(strIP)) return false;
var re = /^(d ).(d ).(d ).(d )$/g //Match IP Regular expression of address
if (re.test(strIP)) {
if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256 ) return true;
}
return false;
}
/*
Purpose: Check whether the input string is empty or all spaces
Input: str
Return :
If all are empty, return true, otherwise return false
*/
function isNull(str) {
if (str == "") return true;
var regu = "^ [ ] $";
var re = new RegExp(regu);
return re.test(str);
}
/*
Purpose: Check whether the value of the input object conforms to an integer Format
Input: str Input string
Return: true if verified, otherwise false
*/
function isInteger(str) {
var regu = /^[-] {0,1}[0-9]{1,}$/;
return regu.test(str);
}
/*
Purpose: Check whether the entered mobile phone number is correct
Input:
s: String
Return:
Return true if verified, false otherwise
*/
function checkMobile(s) {
var regu = /^[ 1][0-9][0-9]{9}$/;
var re = new RegExp(regu);
if (re.test(s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string conforms to the positive integer format
Input:
s: string
Returns:
Returns true if the verification is passed, otherwise returns false
*/
function isNumber(s) {
var regu = "^[0-9] $";
var re = new RegExp(regu);
if (s.search(re) != -1) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string is in a numeric format with decimals, it can be a negative number
Input:
s: string
Return:
If the verification is passed Return true, otherwise return false
*/
function isDecimal(str) {
if (isInteger(str)) return true;
var re = /^[-]{0,1}( d )[.] (d )$/;
if (re.test(str)) {
if (RegExp.$1 == 0 && RegExp.$2 == 0) return false;
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the value of the input object conforms to the port number format
Input: str input String
returns: true if verified, false otherwise
*/
function isPort(str) {
return (isNumber(str) && str < 65536);
}
/*
Purpose: Check whether the value of the input object conforms to the E-Mail format
Input: str input string
Return: if it passes the verification, it returns true, otherwise it returns false
*/
function isEmail(str) {
var myReg = /^[-_A-Za-z0-9] @([_A-Za-z0-9] .) [A-Za-z0-9]{ 2,3}$/;
if (myReg.test(str)) return true;
return false;
}
/*
Purpose: Check whether the input string conforms to the amount format
The format is defined as a positive number with decimals, up to three digits after the decimal point
Input:
s: string
Return:
If it passes verification, it returns true, otherwise it returns false
* /
function isMoney(s) {
var regu = "^[0-9] [.][0-9]{0,3}$";
var re = new RegExp(regu) ;
if (re.test(s)) {
return true;
} else {
return false;
}
}
/*
Usage: Check whether the input string only consists of English letters, numbers and underscores
Input:
s: string
Return:
If it passes the verification, it returns true, otherwise it returns false
*/
function isNumberOr_Letter(s) {//Determine whether it is a number or letter
var regu = "^[0-9a-zA-Z_] $";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check input string Whether it consists of only English letters and numbers
Input:
s: String
Return:
Returns true if it passes verification, otherwise returns false
*/
function isNumberOrLetter(s) {//Determine whether it is a number or letter
var regu = "^[0-9a-zA-Z] $";
var re = new RegExp(regu);
if (re.test( s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string only consists of Chinese characters and letters , composed of numbers
Input:
value: string
Return:
If it passes verification, it returns true, otherwise it returns false
*/
function isChinaOrNumbOrLett(s) {//Judge whether It is composed of Chinese characters, letters and numbers
var regu = "^[0-9a-zA-Zu4e00-u9fa5] $";
var re = new RegExp(regu);
if (re.test( s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Determine whether it is a date
Input: date: date; fmt: date format
Return: return true if it passes verification, otherwise return false
*/
function isDate(date, fmt) {
if (fmt == null) fmt = "yyyyMMdd";
var yIndex = fmt.indexOf("yyyy");
if (yIndex == -1) return false;
var year = date.substring(yIndex, yIndex 4);
var mIndex = fmt.indexOf("MM");
if (mIndex == -1) return false;
var month = date.substring(mIndex, mIndex 2);
var dIndex = fmt.indexOf("dd");
if (dIndex == -1) return false;
var day = date. substring(dIndex, dIndex 2);
if (!isNumber(year) || year > "2100" || year < "1900") return false;
if (!isNumber(month) || month > "12" || month < "01") return false;
if (day > getMaxDay(year, month) || day < "01") return false;
return true;
}
function getMaxDay(year, month) {
if (month == 4 || month == 6 || month == 9 || month == 11)
return "30" ;
if (month == 2)
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return "29";
else
return "28";
return "31";
}
/*
Purpose: Whether character 1 ends with string 2
Input: str1: string; str2: The contained string
returns: true if verified, otherwise false
*/
function isLastMatch(str1, str2) {
var index = str1.lastIndexOf(str2);
if (str1.length == index str2.length) return true;
return false;
}
/*
Purpose: Whether character 1 starts with string 2
Input: str1 : String; str2: Contained string
Return: true if verified, otherwise false
*/
function isFirstMatch(str1, str2) {
var index = str1.indexOf (str2);
if (index == 0) return true;
return false;
}
/*
Purpose: Character 1 is the containing string 2
Input: str1 : String; str2: Contained string
Return: true if verified, otherwise false
*/
function isMatch(str1, str2) {
var index = str1.indexOf (str2);
if (index == -1) return false;
return true;
}
/*
Purpose: Check whether the entered start and end dates are correct, the rules are two The format of the date is correct,
and the end is as scheduled> = start date
Input:
startDate: start date, string
endDate: end as scheduled, string
Return:
Returns true if it passes validation, otherwise returns false
*/
function checkTwoDate(startDate, endDate) {
if (!isDate(startDate)) {
alert("The start date is incorrect! ");
return false;
} else if (!isDate(endDate)) {
alert("The end date is incorrect!");
return false;
} else if ( startDate > endDate) {
alert("The start date cannot be greater than the end date!");
return false;
}
return true;
}
/*
Purpose: Check whether the entered Email mailbox format is correct
Input:
strEmail: string
Return:
If it passes verification, it returns true, otherwise it returns false
*/
function checkEmail (strEmail) {
//var emailReg = /^[_a-z0-9] @([_a-z0-9] .) [a-z0-9]{2,3}$/;
var emailReg = /^[w-] (.[w-] )*@[w-] (.[w-] ) $/;
if (emailReg.test(strEmail)) {
return true ;
} else {
alert("The email address format you entered is incorrect! ");
return false;
}
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template