JavaScript-Common Regular expression code

Source: Internet
Author: User

Web page Special effects common regular expression code

Html

<p>
<label for= "Mobile_phone" > Mobile </label>
<input type= "text" id= "Mobile_phone", name= "Mobile_phone" value= "15107105287"/>
<input type= "button" value= "Validate" onclick= "IsMobile ()"/>
</p>
<p>
<label for= "Phone" > Landline </label>
<input type= "text" id= "phone" name= "Phone" value= "027-87767676"/>
<input type= "button" value= "Validate" onclick= "Isphone ()"/>
</p>
<p>
<label for= "Email" > Email </label>
<input type= "" id= "email" name= "email" value= "zhangchen2397@126.com"/>
<input type= "button" value= "Validate" onclick= "Isemail ()"/>
</p>

Js

function IsMobile () {
var mobile = document.getElementById ("Mobile_phone");
var num = Mobile.value;
var reg =/^ (13[0-9]|186|188|150|151|158|159|147) d{8}$/;
if (num = "") {
Alert ("Please enter full cell phone number");
Mobile.focus ();
return false;
else if (reg.test (num)) {
Alert ("Input cell phone number is in the correct format");
} else {
Alert ("Please enter the correct 11-digit mobile phone number");
Mobile.focus ();
return false;
}
}
function Isemail () {
var email = document.getElementById ("email");
var email_value = Email.value;
if (Email_value = = "") {
Alert ("Please enter a Full mailbox");
Email.focus ();
return false;
} else {
var reg =/^[a-za-z0-9] (w) +@ (W) + (.) + (com|com.cn|net|cn|net.cn|org|biz|info|gov|gov.cn|edu|edu.cn) $/;
if (Reg.test (Email_value)) {
Alert ("Input mailbox format is correct");
} else {
Alert ("Please enter the correct mailbox format");
Email.focus ();
return false;
}
}
}
function Isphone () {
var phone = document.getElementById ("Phone");
var phone_value = Phone.value;
if (Phone_value = = "") {
Alert ("Please enter full landline number");
Phone.focus ();
return false;
} else {
var reg =/^[(]?0d{2,3}[)]?s*[-]?s*d{7,8}$/; 010-87989898 01098989898 (0712) 8989898 010-23343434 The landline numbers for these formats are met
if (Reg.test (Phone_value)) {
Alert ("Input landline number is correct");
} else {
Alert ("Input landline number format error");
Phone.focus ();
return false;
}
}
}

Common regular

•. finds a single character, except for newline and line terminator;
W matches symbols such as letters, Chinese characters, numbers, underscores,
s matching whitespace (including spaces, tabs, and so on),
D matches numbers,
B matches at the beginning or end of a word, and
often The quantifiers used are:
^n matches any string that starts with N,
n$ matches any string that has a trailing n,
n+ matches any string containing at least one n,
n* matches any string containing 0 or more n;
N? Matches any string containing 0 or one n, the
n{x} matches a string containing a sequence of x N,
n{x, and Y} matches a string containing a sequence of x or y n;
Simple example, primarily used to verify mobile phone numbers, phone numbers, and mailboxes:

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.