Regular expressions in JavaScript (recommended) _ Regular expressions

Source: Internet
Author: User
Tags in python

The approximate matching process for regular expressions is to take out the expressions and the character comparisons in the text, and if each character matches, the match succeeds, and the match fails if there is a matching unsuccessful character.

Regular expressions are typically used to find matching strings in text. The quantitative word in Python is greedy by default (in a few languages it may also be the default and not greedy), always trying to match as many characters as possible; instead of greedy, always try to match as few characters as possible. For example, if the regular expression "ab*" is used to find "ABBBC", "abbb" will be found. And if you use a non greedy number word "ab*", you will find "a".

var Pageindex=document.getelementbyid ("TextField"). Value;
 if (!/^[0-9]+$/.test (PageIndex)) {alert ("Please enter a positive integer"); 
return false; 
else{alert ("Input is a positive integer");}/Determine if the input is an empty function IsNull () {var str = document.getElementById (' str '). Value.trim (); if (str.length==0) {alert (' Sorry, the text box cannot be empty or a space! '); 
/Please change the text box to the name of the property you want to verify! 
}//Determine if the date type is YYYY-MM-DD format type function IsDate () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/; 
var r = Str.match (reg); if (r==null) alert (' Sorry, the date you entered is not in the correct format! '); 
Please change the "date" to the name of the property you want to verify! 
}//Determine if the date type is a type YYYY-MM-DD hh:mm:ss Format function isdatetime () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/; 
var r = Str.match (reg); if (r==null) alert (' Sorry, the date you entered is not in the correct format! '); 
Please change the "date" to the name of the property you want to verify! }//Determine if the date type is HH:MM:SS format type function istime () {var str = document.getElementById (' str '). value.tRim (); if (str.length!=0) {reg=/^ (20|21|22|23|[ 0-1]\d) \:[0-5][0-9]) (\:[0-5][0-9])? $/if (!reg.test (str)) {alert ("Sorry, the date you entered is not in the correct format!"); 
/Please change the "date" to the name of the property you need to verify! 
}}//Determine if the character entered is an English letter function isletter () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[a-za-z]+$/; if (!reg.test (str)) {alert ("Sorry, the English letter type you entered is not in the correct format!"); 
/Please change the "English letter type" to the name of the property you need to verify! 
}}//Determine if the character entered is an integer function Isinteger () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[-+]?\d*$/; if (!reg.test (str)) {alert ("Sorry, the integer type you entered is not in the correct format!"); 
/Please replace "integer type" with the name of the property you want to verify! 
}}//Determine if the character entered is a double-precision function isdouble (val) {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[-\+]?\d+ (\.\d+)? $/; if (!reg.test (str)) {alert ("Sorry, the Double-precision type you entered is not in the correct format!"); 
/Please replace "double type" with the name of the property you want to verify! 
}}//Determine if the character entered is: A-z,a-z,0-9 function isstring () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[a-za-z0-9_]+$/; If(!reg.test (str)) {Alert ("Sorry, the string type you typed is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
}}//Determine if the character entered is Chinese function ischinese () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[\u0391-\uffe5]+$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
}//Determine if the email format entered is correct function isemail () {var str = document.getElementById (' str '). Value.trim (); if (str.length!=0) {reg=/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] 
\w+) *$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
The correct function iszip () {var str = document.getElementById (' str ') is judged by the input ZIP code (only six digits). Value.trim (); 
if (str.length!=0) {reg=/^\d{6}$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
The number entered is not greater than a specific number function MaxValue () {var val = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[-+]?\d*$/; if (!reg.test (str)) {//To determine if the number type is (Val>parseint (' 123 '))//"123" for oneself set the maximum value {alert (' Sorry, you entered the number out of range ');//Please change the "number" to the name of the property you want to verify!  } 
} 
} 
}

Phone:/^ ((\d{2,3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7} (\-\d{1,4})? $/

Mobile:/^ ((\d{2,3}\) | ( \d{3}\-))? 13\d{9}$/

URL:/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "\"]) *$/

Idcard:/^\d{15} (\d{2}[a-za-z0-9])? $/

QQ:/^[1-9]\d{4,8}$/

A special Amount:/^ ((\d{1,3} (, \d{3}) *) | ( \d+)) (\.\d{2})? $///Description: In addition to the "XXX xx,xxx xx,xxx.00" format

Provide each JS verification method above. Trim () property

String.prototype.trim=function () {return 
this.replace (^\s*) | ( \s*$)/g, ""); 

Call:

<input type= "text" name= "str" >

<input type= "button" value= "OK" onclick= ">//onclick write the JS validation function you want to call

JS Verify form JS Submit Validation class

Additional: JS Verify Radio is selected

<script language= "javascript" >
function checkform (obj)
{for
(i=0;i<obj.oo.length;i++)
 if (obj.oo[i].checked==true) return true;
Alert ("Please select") return
false;
</script>
<form id= "Form1" Name= "Form1" method= "Post" action= "" onsubmit= "return Checkform (This) > <input type= "Radio" Name= oo "value=" RadioButton "/> <input type=" Radio "name="
 oo "value=" RadioButton "/> <input type=" Submission "name=" Submit "value="
 submitted "/>
</form>

The above content is small series to introduce the JS regular expression of the relevant knowledge, I hope to help you!

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.