12 Common JS Regular Expressions _ Regular expressions

Source: Internet
Author: User
Tags getdate

In this article, I've written 12 very useful regular expressions, which is a favorite of web developers.

1. In the input box can only enter the amount, in fact, can only enter a maximum of two decimal digits

The first type in the input box restricts 
 <input type= "text" maxlength= "8" class= "Form-control" id= "Amount" style= "margin-right:2px". Value= "" onchange= "count ();" onkeyup= "if (this.value==this.value2) return; if (This.value.search/^\d* (?: \. \d{0,2}) $/) ==-1) this.value= (this.value2) this.value2: "; else this.value2=this.value;" > 
//The second way to add dynamic forms to a form is only validated in the JS method. 
 var amount=$ ("#amount"). Val (); 
 if (Amount.search/^\d* (?: \. \d{0,2}) ($/) ==-1) { 
   alert ("The amount is not formatted, up to two decimal places"); 
   return false; 

2. Verify the mailbox format

var reg=/\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * * *; 
 var email=$ ("#email"). Val (); 
 if (!reg.test (email)) { 
    alert ("Please enter the mailbox account that complies with the specification!") "); 
    return false; 
   } 

3. The password uses the numeral, the letter, the special character and the length is 8-20 bits

function Validatepwd (str) { 
if (/^.*?[ \d]+.*$/.test (str) &&/^.*? [A-za-z]/.test (str) 
&&/^.*?[ ~/'!@#$%^&* () _+| {}?;:><\-\]\\[\/].*$/.test (str) &&/^. {8,20}$/.test (str)) {return 
true; 
} 
return false; 
} 

4. Verify Telephone number

/** 
 * Verify phone number * 
 @param phonevalue number to verify 
 * @returns match returns True mismatch return false/ 
function Validatephone (phonevalue) { 
 phonevalue = Valuetrim (phonevalue); 
 var reg =/^[1][0-9]{10}$/; 
 Return Reg.test (Phonevalue); 
} 

5. Judge whether it is Chinese characters

/** 
 * Judge whether it is Chinese characters 
 * 
 * @param charvalue 
 *   to validate the data 
 * @returns match returns true mismatch return 
 false 
* * * function Ischaracter (charvalue) { 
 var reg =/^[\u4e00-\u9fa5]{0,}$/; 
 Return Reg.test (charvalue); 
} 

6. Is the letter: true: Yes, false: No

function Ischar (charvalue) { 
 var charpattern=/^[a-za-z]*$/;//is the letter 
 result=charpattern.test (charvalue); 
 return result; 
} 

7. Determining whether a number

function Isnum (numvalue) { 
 var numpattern=/^\d*$/;//number Regular expression 
 result=numpattern.test (NumValue); 
 return result; 
} 

8. The regular expression of an integer

function Isint (intvalue) { 
  var intpattern=/^0$|^[1-9]\d*$/;//integer Regular expression 
 result=intpattern.test (intvalue); 
  return result; 
 } 

9. Letters and Numbers

 function Ischarnum (flagvalue) { 
 var flagpattern=/^[a-za-z0-9]*$/;//is a letter and number 
 result=flagpattern.test ( Flagvalue); 
 return result; 
} 

10. Test 18 ID Number

/** 
 * Test 18 ID Number (15 digits can only detect the birthday is correct, to solve themselves) 
 * * 
 @param idcardvalue 
 *   18 ID Card number 
 * @returns Match return True mismatch return false 
/function idcardvildate (CID) { 
var arrexp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5 , 8, 4, 2];//weighted factor 
var arrvalid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//check code 
var reg =/^[1-9]\d{5}[1-9]\d{3} ((0\d) | (1[0-2]) ([[0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/; 
if (Reg.test (CID)) { 
var sum = 0, idx; 
for (var i = 0; i < cid.length-1 i++) { 
//pair of first 17 digits with weight product sum 
sum + = parseint (Cid.substr (i, 1), ten) * arrexp[i ]; 
} 
Computational modulus (fixed algorithm) 
idx = sum%; 
Check whether the 18th is equal to check code return 
Arrvalid[idx] = = CID.SUBSTR (1). toUpperCase (); 
else {return 
false; 
} 
} 

11. Verify that the birthday in the 15-digit ID number is a valid birthday

function Isvaliditybrithby15idcard (idCard15) { 
 var year = idcard15.substring (6, 8); 
 var month = idcard15.substring (8); 
 var day = idcard15.substring (a); 
 var temp_date = new Date (year, parsefloat (month)-1, parsefloat (day)); 
 The age of your old ID card does not need to consider the millennium bug problem and use the getyear () method 
 if (Temp_date.getyear ()!= parsefloat (year) 
   | | temp_date.getmonth ()! = parsefloat (month)-1 
   | | | temp_date.getdate ()!= parsefloat (day) {return 
  false; 
 } else {return 
  true ; 
 } 

12. Verify that the birthday in the 18-digit ID number is a valid birthday

function Isvaliditybrithby18idcard (idCard18) { 
 var year = idcard18.substring (6); 
 var month = idcard18.substring (a); 
 var day = idcard18.substring (a); 
 var temp_date = new Date (year, parsefloat (month)-1, parsefloat (day)); 
 Here use getFullYear () to get the year to avoid the bug problem 
 if (temp_date.getfullyear ()!= parsefloat () 
   | | temp_date.getmonth ()!= Parsefloat (month)-1 
   | | | temp_date.getdate ()!= parsefloat (day)) {return 
  false; 
 } else {return 
  true; 
 } 
}

This is the small collation of the web development commonly used in the form of validation of the regular expression, I hope you like.

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.