Common validation in Javascript, such as domain name, mobile phone, email, etc.

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Verify the general domain name verification
Function checkNetURL (){
Var netURL = document. getElementById ("netURL ");
// Verify common domain names such as www.baidu.com
Var regStr = "^ ([a-zA-Z0-9-] + \.) {1,} (com | net | edu | miz | biz | cn | cc) $ ";
Var re = new RegExp (regStr); // create a regular expression object
// Var re =/^ ([a-zA-Z0-9-] + \.) {1,} (com | net | edu | miz | biz | cn | cc) $ /;
If (! Re.exe c (netURL. value) {// verify that the entered string complies with the rules
Alert ("the URL you entered is incorrect! The correct format is mail.brookes.com ");
NetURL. focus ();
Return;
}
}
// Mobile phone Verification
Function iphoneRegExp (){
Var iphone = document. getElementById ("iphone"). value;
Var regStr = "^ (13 [0-9]) | (14 [0-9]) | (15 [^ 4, \ D]) | (18 [0-9]) \ d {8} $ ";
Var re = new RegExp (regStr );
If (! Re. test (iphone )){
Alert ("type error, correct format such as 152636363 ** (or mobile phone number starting with 13, 14, 18 )");
}
}
// Email verification (some bugs)
/* Function emailRegExp (){
Var email = document. getElementById ("email"). value;
Var regStr = "^ [a-zA-Z0-9 \ u4E00-\ u9FA5] * [-_]? [A-zA-Z0-9]) {1,} + @ ([a-zA-Z0-9] * [-_]? [A-zA-Z0-9] +) + [\.] [A-Za-z] {2, 3} ([\.] [A-Za-z] {2 })? $ ";
Var re = new RegExp (regStr );
If (! Re. test (email )){
Alert ("type error, correct format such as 152636363 ** (or mobile phone number starting with 13, 14, 18 )");
// Iphone. focus ();
}
}*/
// ID card verification
Function cardRegExp (){
Var card = document. getElementById ("card"). value;
Var regStr = "(^ \ d {15} $) | (^ \ d {18} $) | (^ \ d {17} (\ d | X | x) $ )";
Var re = new RegExp (regStr );
If (! Re. test (card )){
Alert ("the input is invalid. The correct format is 4101821897020356 **");
}
}
// Hong Kong and Macao Province Certificate
Function gangAocardRegExp (){
Var card = document. getElementById ("card"). value;
Var regStr = "[A-Z] {} [0-9] {6} [(] {1} [A-Z0-9] {1} [)] {1 }";
Var re = new RegExp (regStr );
If (! Re. test (card )){
Alert ("illegal ID card for residents of Hong Kong and Macao! Correct format: A15263 * (E )");
}
}
// Disability certificate
Function canJicardRegExp (){
Var card = document. getElementById ("card"). value;
Var regStr = "(^ \ d {22} $) | (^ \ d {20} $ )";
Var re = new RegExp (regStr );
If (! Re. test (card )){
Alert ("the disability certificate input is illegal! ");
}
}
// Zip code
Function postCardRegExp (){
Var postCard = document. getElementById ("postCard"). value;
Var regStr = "^ [0-9] {6} $ ";
Var re = new RegExp (regStr );
If (! Re. test (postCard )){
Alert ("the input is invalid! The correct format is 4500! ");
}
}
// Fax and landline Verification
Function telRegExp (){
Var tel = document. getElementById ("tel"). value;
Var regStr = "(^ \ d {11} $) | (^ \ d {12} $ )) | (^ \ d {3}-\ d {8} $) | (^ \ d {4}-\ d {7} $) | (^ \ d {4}-\ d {8} $ )";
Var re = new RegExp (regStr );
If (! Re. test (tel )){
Alert ("Incorrect writing format! The correct format is 0511-44052 ** or 021-878888 **");
}
}
// User name verification. The rules are as follows:
// Check whether the matched account is valid. The account must start with a letter and contain 5-16 bytes. The account must contain letters, numbers, and underscores.
Function userNameRegExp (){
Var userName = document. getElementById ("userName"). value;
Var regStr = "^ [a-zA-Z] [a-zA-Z0-9 _] {} $ ";
Var re = new RegExp (regStr );
If (! Re. test (userName )){
Alert ("must begin with a letter, followed by 4-15 digits, letters or underscores! ");
}
}
// Check the integer
Function numRegExp (){
Var num = document. getElementById ("num"). value;
Var regStr = "^ [0-9] * [1-9] [0-9] * $ ";
Var re = new RegExp (regStr );
If (! Re. test (num )){
Alert ("Incorrect format, for example, 8080 ");
}
}
// Verify the decimal or integer
Function numFontRegExp (){
Var num = document. getElementById ("num"). value;
Var regStr = "^ ([0] {1,}) | ([0-9] {1,}) [.] {1} [0-9] {1,}) | [0-9] {1,} $ ";
Var re = new RegExp (regStr );
If (! Re. test (num )){
Alert ("Incorrect format, correct format: 12.3 ");
}
}
// Verify the special character ^ [^ % $ & @] {1,} $
Function strRegExp (){
Var str = document. getElementById ("str"). value;
Var regStr = "^ [^ % $ & @] {1, }$ ";
Var re = new RegExp (regStr );
If (! Re. test (str )){
Alert ("cannot contain special characters ");
}
}
// Verify the real name
Function nameRegExp (){
Var name = document. getElementById ("name"). value;
Var regStr = "(^ [a-zA-Z] {1} ([a-zA-Z _]) {2, 20 }) | (^ [\ u4E00-\ u9FA5] {1} + [a-zA-Z0-9 \ u4E00-\ u9FA5] {}) $ ";
Var re = new RegExp (regStr );
If (! Re. test (name )){
Alert ("must be 2-15 Chinese characters or 3-21 English letters ");
}
}
//// Verify your passport
Function huzhoaRegExp (){
Var huzhao = document. getElementById ("huzhao"). value;
Var regStr = "^ (14) | (15) [0-9] {7}) | (G | S | D [0-9] {8 }) | (P .) | (S .) [0-9] {7}) $ ";
Var re = new RegExp (regStr );
If (! Re. test (huzhao )){
Alert ("illegal passport input, correct format: G00106556 ");
}
}
// Military ID card
Function junGuanRegExp (){
Var junguan = document. getElementById ("junguan"). value;
Var regStr = "^ [\ u4E00-\ u9FA5] {1} \ d {7} $ ";
Var re = new RegExp (regStr );
If (! Re. test (junguan )){
Alert ("the input is invalid! Correct format: Sea 6042 **");
}
}
// Civilian cadre Verification
Function junGuanRegExp (){
Var wenzhi = document. getElementById ("wenzhi"). value;
Var regStr = "^ [\ u4E00-\ u9FA5] {1} 文\ \ d {4, 12} $ ";
Var re = new RegExp (regStr );
If (! Re. test (wenzhi )){
Alert ("invalid civilian cadre certificate input! Correct format: guangwen 0816 **");
}
}
// Secret Certificate
Function shiBingRegExp (){
Var shibing = document. getElementById ("shibing"). value;
Var regStr = "^ [\ u4E00-\ u9FA5] {1, 3} \ d {4, 12} $ ";
Var re = new RegExp (regStr );
If (! Re. test (shibing )){
Alert ("the secret certificate input is invalid! Correct format: E 176340 **");
}
}
// Mobile phone and landline Verification
Function iphoneTelRegExp (){
Var iphoneTel = document. getElementById ("iphoneTel"). value;
Var regStr = "(^ \ d {11} $) | (^ \ d {12} $ )) | (^ \ d {3}-\ d {8} $) | (^ \ d {4}-\ d {7} $) | (^ \ d {4}-\ d {8} $) | (^ (13 [0-9]) | (14 [0-9]) | (15 [^ 4, \ D]) | (18 [0-9]) \ d {8} $) $ ";
Var re = new RegExp (regStr );
If (! Re. test (iphoneTel )){
Alert ("the phone number is incorrectly written and does not meet the phone number specification (for example, 0511-4255xxx or 15236565xxx). Please fill in again ");
}
}
</Script>

Related Article

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.