Home >Web Front-end >JS Tutorial >js uses regular expressions to validate forms (a relatively complete resource)_javascript skills

js uses regular expressions to validate forms (a relatively complete resource)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:441168browse

I’m learning form validation on web pages, and learning regular expressions by the way

After searching online, I found a relatively complete resource, and did a little layout

Copy code The code is as follows:

// Check whether it is a valid real name, which can only contain Chinese or uppercase English letters
function isValidTrueName(strName){
var str = Trim(strName);
//Determine whether it is all English uppercase or all Chinese, and can include spaces
var reg = /^[A-Z u4E00-u9FA5] $/;
if(reg.test(str)){
return false;
}
return true;
}

JavaScript form Age verification
JavaScript form verification of age, which determines whether an input amount matches the age, is implemented through regular expressions.
Copy code The code is as follows:

//Check age

function isAge (str){
var mydate=new Date;
var now=mydate.getFullYear();
if (str < now-60 || str > now-18){
return false;
}
return true;
}

JavaScript form verification phone number
JavaScript form verification phone number to determine whether an input is the phone number, implemented through regular expressions.
Copy code The code is as follows:

//Check phone number
function isTel(str){
var reg=/^([0-9]| [-]) $/g ;
if(str.length18){
return false;
}
else{
return reg.exec(str);
}
}


Regular expression verification email
JavaScript form verification email, determine whether an input is a mailbox email, implemented through regular expressions.
Copy code The code is as follows:

//Check email mailbox

function isEmail(str){

var reg = /^([a-zA-Z0-9_-]) @([a-zA-Z0-9_-]) ((.[a-zA-Z0 -9_-]{2,3}){1,2})$/;

return reg.test(str);

}

JavaScript form verification Chinese capital letters
JavaScript form verification Chinese capital letters, determine whether an input is Chinese or capital English letters, implemented through regular expressions.
Copy code The code is as follows:

// Check whether it is a valid real name, only Contains Chinese or uppercase English letters

function isValidTrueName(strName){
var str = Trim(strName);
//Determine whether it is all English uppercase or all Chinese, and can contain spaces
var reg = /^[A-Z u4E00-u9FA5] $/;
if(reg.test(str)){
return false;
}
return true;
}

JavaScript Verification
JavaScript Form Verification Age
JavaScript Form Verification Age, which determines whether an input amount matches the age, is implemented through regular expressions.
Copy code The code is as follows:

//Check age

function isAge (str){

var mydate=new Date;

var now=mydate.getFullYear();

if (str < now-60 || str > now-18){
return false;
}
return true;
}

For time and date functions, you can refer to the article "getDate date function in JavaScript"

JavaScript form validates Chinese capital letters
JavaScript form validates Chinese capital letters and determines whether an input is Chinese or uppercase English letters, implemented through regular expressions.
Copy code The code is as follows:

// Check whether it is a valid real name, only Contains Chinese or uppercase English letters

function isValidTrueName(strName){

var str = Trim(strName);

//Determine whether it is all English uppercase or all Chinese , can contain spaces

var reg = /^[A-Z u4E00-u9FA5] $/;

if(reg.test(str)){

return false;

}

return true;

}

JavaScript form verification whether it is Chinese
JavaScript form verification whether it is Chinese, judging whether an input is Chinese, implemented through regular expressions
Copy code The code is as follows:

// Check whether it is Chinese
function isChn(str){
var reg = /^ $ /;
if(!reg.test(str)){
return false;
}
return true;
}

JavaScript form Verify password
JavaScript form verification password is to check whether the input box is a valid password.
Passwords are only allowed to consist of ascii.
This function is only used when modifying or registering passwords.
That is to say, any string that is not composed of ascii cannot pass verification.
Please see the demo code below for the specific function checkValidPasswd
Copy the code The code is as follows:

function checkValidPasswd(str){
var reg = /^[x00-x7f] $/;
if (! reg.test(str)){
return false;
}
if (str.length < 6 || str.length > 16){
return false;
}
return true;
}

JavaScript Regular verification IP
Copy code The code is as follows:

JavaScript regular verification IP
JavaScript regular verification IP, purpose: verify the format of the ip address
Input: strIP: ip address
Return: If JavaScript returns true by verifying the IP, otherwise it returns false;

JavaScript verification IP code As follows

function isIP(strIP) {
if (isNull(strIP)) return false;
var re=/^(d ).(d ).(d ).(d )$ /g //Regular expression matching IP address
if(re.test(strIP))
{
if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3< ;256 && RegExp.$4<256) return true;
}
return false;
}

Use regular rules to determine whether the obtained IP address is in the format, and then return the corresponding Result
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