JS Regular Expression Validation _ regular expression

Source: Internet
Author: User
Tags trim zip
Determines whether 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 verifySaid! 
}}//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 entered 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)) {//Judge whether the number type if (Val>parseint (' 123 '))//"123" sets the maximum value for itself {alert (' Sorry, the number you entered is out of range ');//Please add "number" 
Change 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})?/; 
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 entered 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 typed 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)) {//Judge whether the number type if (Val>parseint (' 123 '))//"123" sets the maximum value for itself {alert (' Sorry, the number you entered is out of range ');//Please add "number" 
Change 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:/^ ((\d2,3\d2,3) | ( \d{3}\-))? 13\d{9}/url:/http:\/\/[a−za−z0−9]+\. [a−za−z0−9]+[\/=\?/url:/http:\/\/[a−za−z0−9]+\.[  a−za−z0−9]+[\/=\?/Idcard:/^\d{15} (\d{2}[a-za-z0-9)?/qq:/[1−9]\d4,8/qq:/[1−9]\d4,8/

A special Amount:/^ ((\d{1,3} (, \d{3}) *) | ( \d+)) (\.\d{2})? $/

Note: In addition to the "XXX xx,xxx xx,xxx.00" format
for the above provide each JS verification method provided. 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

The above is a small set to introduce the JS regular expression of the verification, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!

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.