JS uses regular expressions to validate forms (more complete resources) _javascript tips

Source: Internet
Author: User
Tags time and date

In the Learning Web page of the form validation, by the way to learn the regular expression

Found a relatively complete resources on the Internet, a little bit of typesetting

Copy Code code as follows:

Check whether it is a valid real name and can only contain Chinese or uppercase letters in English.
function Isvalidtruename (strName) {
var str = Trim (strName);
To determine whether it is all English or Chinese, you can include spaces
var reg =/^[a-z u4e00-u9fa5]+$/;
if (Reg.test (str)) {
return false;
}
return true;
}

JavaScript form authentication Age
JavaScript form validation age, to determine whether an input is in line with age, implemented through regular expressions.
Copy Code code 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 authentication phone number
The JavaScript form verifies the phone number to determine whether an input is a phone number and is implemented through a regular expression.
Copy Code code as follows:

<span style= "font-size:12px" >//Check phone number
</span><span style= "font-size:10px" >function Istel (str) {
var reg=/^ ([0-9]|[ \-]) +$/g;
if (str.length18) {
return false;
}
else{
return reg.exec (str);
}
}</span>

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

Check email Email

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 validation Chinese capital letters
JavaScript forms validate Chinese capital letters to determine whether an input is an English letter in Chinese or uppercase, and is implemented through regular expressions.
Copy Code code as follows:

Check whether it is a valid real name and can only contain Chinese or uppercase letters in English.

function Isvalidtruename (strName) {
var str = Trim (strName);
To determine whether it is all English or Chinese, you can include spaces
var reg =/^[a-z u4e00-u9fa5]+$/;
if (Reg.test (str)) {
return false;
}
return true;
}

JavaScript Validation
JavaScript form authentication Age
JavaScript form validation age, to determine whether an input is in line with age, implemented through regular expressions.
Copy Code code 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;
}

About the time and date function, you can refer to the next "JavaScript getdate Date function" This article

JavaScript Form validation Chinese capital letters
JavaScript forms validate Chinese capital letters to determine whether an input is an English letter in Chinese or uppercase, and is implemented through regular expressions.
Copy Code code as follows:

Check whether it is a valid real name and can only contain Chinese or uppercase letters in English.

function Isvalidtruename (strName) {

var str = Trim (strName);

To determine whether it is all English or Chinese, you can include spaces

var reg =/^[a-z u4e00-u9fa5]+$/;

if (Reg.test (str)) {

return false;

}

return true;

}

javascript Form validation is Chinese
JavaScript form verification is Chinese, to determine whether an input is Chinese, through regular expressions to implement
Copy Code code as follows:

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

JavaScript form authentication password
JavaScript form authentication password is to check if the input box is a valid password.
The password is only allowed to consist of ASCII,
This function is only used when modifying or registering a password.
That means that everything is not ASCII. A string cannot pass validation.
Specific function checkvalidpasswd Please see the demo code below
Copy Code code 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 authentication IP
Copy Code code as follows:

JavaScript regular authentication IP
JavaScript regular authentication IP, purpose: Verify the format of IP address
Input: Strip:ip Address
Return: False if JavaScript returns true by verifying IP;

JavaScript verifies IP code as follows

function IsIP (StrIP) {
if (IsNull (StrIP)) return false;
var re=/^ (\d+) \. (\d+) \. (\d+) \. (\d+) $/g//matching the regular expression of the IP address
if (Re.test (StrIP))
{
if (regexp.$1 <256 && regexp.$2<256 && regexp.$3<256 && regexp.$4<256) return true;
}
return false;
}

Through the regular to determine whether the access is the IP address format, and then return the corresponding results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.