JS verification of Regular Expressions

Source: Internet
Author: User

/Determine whether the input content is empty
Function isnull (){
VaR STR = Document. getelementbyid ('str'). value. Trim ();
If (Str. Length = 0 ){
Alert ('Sorry, the text box cannot be blank or blank! '); // Change the "text box" to the attribute name you want to verify!
}
}

// Determine whether the date type is a type in YYYY-MM-DD format
Function isdate (){
VaR STR = Document. getelementbyid ('str'). value. Trim ();
If (Str. length! = 0 ){
VaR Reg =/^ (\ D {}) (-| \/) (\ D {}) \ 2 (\ D {}) $ /;
VaR r = Str. Match (REG );
If (r = NULL)
Alert ('Sorry, the date format you entered is incorrect! '); // Change "date" to the name of the property to be verified!
}
}

// Determine whether the date type is YYYY-MM-DD hh: mm: SS type
Function isdatetime (){
VaR STR = Document. getelementbyid ('str'). value. Trim ();
If (Str. length! = 0 ){
VaR Reg =/^ (\ D {}) (-| \/) (\ D {}) \ 2 (\ D }) (\ D {1, 2}) :( \ D {1, 2}) :( \ D {1, 2}) $ /;
VaR r = Str. Match (REG );
If (r = NULL)
Alert ('Sorry, the date format you entered is incorrect! '); // Change "date" to the name of the property to be verified!
}
}

// Determine whether the date type is in HH: mm: SS format
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 format you entered is incorrect! "); // Change" date "to the attribute name you want to verify!
}
}
}

// Determine whether the entered character 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 format you entered is incorrect! "); // Change" English letter type "to the attribute name you want to verify!
}
}
}

// Determine whether the input character 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 format of the integer type you entered is incorrect! "); // Replace" Integer type "with the property name you want to verify!
}
}
}

// Determine whether the input character is 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 format of the Double Precision type you entered is incorrect! "); // Replace" double-precision type "with the property name you want to verify!
}
}
}


// Determine whether the input character 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 format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify!
}
}
}

// Determine whether the input character 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 format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify!
}
}
}

// Determine whether the input email format 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 format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify!
}
}
}

// Determine whether the entered zip code (only six digits can be entered) is correct
Function iszip ()
{
VaR STR = Document. getelementbyid ('str'). value. Trim ();
If (Str. length! = 0 ){
Reg =/^ \ D {6} $ /;
If (! Reg. Test (STR )){
Alert ("sorry, the format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify!
}
}
}

// Judge whether the input number is greater than a specific number.
Function maxvalue ()
{
Var val = Document. getelementbyid ('str'). value. Trim ();
If (Str. length! = 0 ){
Reg =/^ [-+]? \ D * $ /;
If (! Reg. Test (STR) {// determines whether the data type is Numeric.
If (Val> parseint ('000000') // "123" is the maximum value you set.
{
Alert ('Sorry, the number you entered is out of range '); // change "Number" to the attribute name 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: Except for the format "xxx xx, xxx XX, xxx.00"

// Provide. Trim () attributes for each JS verification method provided above
String. Prototype. Trim = function (){
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}

Call:
<Input type = "text" name = "str">
<Input type = "button" value = "OK" onclick = ""> // write the JS verification function to be called in onclick.

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.