Common Regular Expressions

Source: Internet
Author: User

A regular expression that is frequently used for verification written by a person in the project: (for reference only)


// Define two global variables
VaR reg;
VaR errorinfo;

// The input numeric type must be int, and both positive and negative integers are allowed.
Function validate_integer (text, value ){
Reg =/^ [-\ +]? \ D + $ /;
Errorinfo = text + "" + value + "can only be an integer. Please verify and enter it again! ";
Verifybyreg (value, Reg, errorinfo );
}

// The input number type must be double. The reserved decimal places can only be two digits and can be positive or negative.
Function validate_double (text, value ){
Reg =/^ [-\ +]? ([1-9] (\ D + )? | 0) (\. \ D {2}) $ /;
Errorinfo = text + "" + value + "can only retain 2 decimal places. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// The input must be of the numeric type and cannot contain any other character.
Function validate_number (text, value ){
Reg =/^ \ D + $ /;
Errorinfo = text + "" + value + "can only be numbers. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the n-bit numeric type
Function validate_number_n (text, value, n ){
Reg = eval ("/^ \ D {" + N + "} $ /");
Errorinfo = text + "" + value + "can only be" + N + "digits. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the positive number with two decimal places
Function validate_decimal_two (text, value ){
Reg =/^ ([1-9] (\ D + )? | 0) (. [0-9] {2 })? $ /;
Errorinfo = text + "" + value + "can only be a positive number with two decimal places. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify a non-zero positive integer
Function validate_number_positive (text, value ){
Reg =/^ \ +? [1-9] [0-9] * $ /;
Errorinfo = text + "" + value + "can only be a non-zero positive integer. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the non-zero negative integer
Function validate_number_negative (text, value ){
Reg =/^ [-] [1-9] [0-9] * $ /;
Errorinfo = text + "" + value + "can only be a non-zero negative integer. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the non-negative integer, which can be 0
Function validate_positive_haszero (text, value ){
Reg =/^ [1-9] (\ D + )? | 0 $ /;
Errorinfo = text + "" + value + "can only be 0 or a positive integer. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify that the value is not a positive integer and can be 0.
Function validate_negative_haszero (text, value ){
Reg =/^ \-[1-9] (\ D + )? | 0 $ /;
Errorinfo = text + "" + value + "can only be 0 or a negative integer. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the non-negative floating point number (Positive floating point number + 0)
Function validate_float_notnegative (text, value ){
Reg =/^ ([1-9] (\ D + )? | 0) (\. \ D +) | 0 $ /;
Errorinfo = text + "" + value + "can only be 0 or a floating point number. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the Positive floating point number
Function validate_float_positive (text, value ){
Reg =/^ ([1-9] (\ D + )? | 0) (\. \ D +) $ /;
Errorinfo = text + "" + value + "can only be a positive floating point number. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Non-Positive floating point number (negative floating point number + 0)
Function validate_float_notpositive (text, value ){
Reg =/^ \-([1-9] (\ D + )? | 0) (\. \ D +) | 0 $ /;
Errorinfo = text + "" + value + "can only be 0 or a negative floating point number. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );

}

// Verify the negative floating point number
Function validate_float_negative (text, value ){
Reg =/^ \-([1-9] (\ D + )? | 0) (\. \ D +) $ /;
Errorinfo = text + "" + value + "can only be a negative floating point number. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );

}

// Verify the number of decimal places of the Positive floating point number
Function validate_float_posbit (text, value, n ){
Reg = eval ("/^ (\ D +) (\. \ D {" + N + "}) $ /");
Errorinfo = text + "" + value + "can only be" + N + "Positive floating point number. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );

}


// Verify the number of decimal places of the negative floating point number
Function validate_float_negbit (text, value, n ){
Reg = eval ("/^ (-? \ D +) (\. \ D {"+ N +"}) $ /");
Errorinfo = text + "" + value + "can only be" + N + "bit negative floating point number. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );

}

// Check whether the text is empty
Function checknull (text, value ){
If (value = "" | typeof (value) = 'undefined '){
Errorinfo = text + "" + value + "cannot be blank. Please enter it again! ";
Returninfo (errorinfo );
}
}

// Verify a string consisting of 26 English letters
Function validate_string_letter (text, value ){
Reg =/^ [A-Za-Z] + $ /;
Errorinfo = text + "" + value + "can only consist of 26 uppercase/lowercase English letters. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify a string consisting of 26 uppercase letters
Function validate_string_bigletter (text, value ){
Reg =/^ [A-Z] + $ /;
Errorinfo = text + "" + value + "can only consist of 26 uppercase letters. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify a string consisting of 26 lowercase letters
Function validate_string_smallletter (text, value ){
Reg =/^ [A-Z] + $ /;
Errorinfo = text + "" + value + "can only consist of 26 lower-case English letters. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the string consisting of a number and 26 English letters
Function validate_string_number (text, value ){
Reg =/^ [A-Za-z0-9] + $ /;
Errorinfo = text + "" + value + "can only consist of numbers and 26 English letters. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify a string consisting of digits, 26 English letters, or underscores
Function validate_string_numberandunderline (text, value ){
Reg =/^ \ W + $ /;
Errorinfo = text + "" + value + "can only contain numbers, 26 English letters, or underscores. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}


// Verify the email address
Function validate_email (value ){
Reg =/^ [\ W-] + (\. [\ W-] +) * @ [\ W-] + (\. [\ W-] +) + $ /;
Errorinfo = 'mailbox '+ value + "invalid. Please enter ";
Verifybyreg (value, Reg, errorinfo );
}


// If this method is called by default during verification, it can be a positive integer or a positive integer with four valid digits retained
/** Problems
Function validate_money_default (text, value ){
VaR Reg =/^ \ D + (\. \ D {4 })? $ /;
VaR errorinfo = text + "" + value + "can only be a positive integer or retain four valid numbers. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}
**/

// Verify that only Chinese characters are input.
Function validate_chinese (text, value ){
Reg =/^ [\ u4e00-\ u9fa5] + $ /;
Errorinfo = text + "" + value + "can only be Chinese characters. Please enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify that the input can only contain N characters.
Function validate_bitchinese (text, value, bit ){
Reg = eval ("/^ [\ u4e00-\ u9fa5] {" + bit + "} $/"); alert (REG );
Errorinfo = text + "" + value + "can only be" + bit + "Chinese characters. Please enter ";
Verifybyreg (value, Reg, errorinfo );
}

// Compare with the maximum value to see if the number exceeds the upper limit of the system.
Function thanmax (text, number, max ){
If (number> MAX ){
Errorinfo = text + "" + number + "cannot be greater than" + MAX + ". Enter" again ";
Returninfo (errorinfo );
}
}

// Compare with the minimum value to see if the number is lower than the lower limit of the system.
Function thinmin (text, number, min ){
If (number <min ){
Errorinfo = text + "" + number + "cannot be less than" + min + ". Enter" again ";
Returninfo (errorinfo );
}
}

// Whether the string of the input text is within the specified length of the system
Function islimit (text, value, length ){
VaR arr = value. Split ("");
If (ARR. length> length ){
Errorinfo = text + "" + value + "is too long. Enter a maximum of" + Length + "characters ";
Returninfo (errorinfo );
}
}


// Postal code verification
Function validate_zipcode (value ){
Reg =/[1-9] \ D {5 }(?! \ D )/;
Errorinfo = 'postal Code' + value + "Incorrect. Please verify and re-enter ";
Verifybyreg (value, Reg, errorinfo );
}


// Perform simple verification and conversion on numeric data. If this parameter is not set, a JS Code error occurs. Therefore, convert numeric data.
// Determine non-null characters
Function getzerobynull (text, value ){
Errorinfo = text + "" + value + "cannot be blank. Please verify and re-enter ";
If (value = NULL | value = ''| value = 'undefined '){
Returninfo (errorinfo );
}
}


// ID card verification, divided into two types: 15 and 18
Function validate_idcard (value ){
VaR size = value. Split ("");
If (size. Length = 15 ){
Reg =/^ [1-9] \ D {7} (0 \ D) | (1 [0-2]) ([0 | 1 | 2] \ D) | 3 [0-1]) \ D {3} $/; // 15-digit ID card verification
Errorinfo = 'digit ID card number' + value + "Incorrect. Please verify and re-enter ";
} Else if (size. Length = 18 ){
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) $ /; // 18-digit ID card verification
Errorinfo = 'digit ID card number' + value + "Incorrect. Please verify and re-enter ";
} Else {
Errorinfo = "enter a valid ID card number! ";
Returninfo (errorinfo );
}
Verifybyreg (value, Reg, errorinfo );

}


// Call this method for each verification
Function verifybyreg (value, Reg, errorinfo ){
If (value! = ''& Reg! = ''){
VaR Regexp = new Regexp (REG );
If (! Regexp. Test (value )){
Alert (errorinfo );
Throw errorinfo;
}
}
}


// If regular expression verification is not performed, you only need to return information. This method is suitable for internal calls.
Function returninfo (Message ){
Alert (Message );
Throw message;
}

// License number format: QS 1234 2346 3456, where spaces are optional
/// The Business License format is: Hunan 010249, but the Chinese provincial capital may have three characters for short, so the Chinese can be defined as 1-3 Characters
Function validate_license (value, type ){
If (type = 1 ){
Reg =/^ [A-Za-Z] {2} []? (\ D {4} []? \ D {4} []? \ D {4}) $/; // license number of the production enterprise
Errorinfo = "production license number" + value + "error. Please verify and enter it again! ";
} Else if (type = 2 ){
Reg =/^ [\ u4e00-\ u9fa5] {1, 3} \ D {6} $ /;
Errorinfo = "Business License number" + value + "error. Please verify and enter it again! ";
}
Verifybyreg (value, Reg, errorinfo );
}


// Strings that can be matched by fax, for example, + 123-999 999; + 123-999 999; 123 999 999; + 123 999999
Function validate_fax (value ){
Reg =/^ ([+] {0, 1 })? \ D {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
Error errorinfo = "fax" + value + ". Please try again! ";
Verifybyreg (value, Reg, errorinfo );
}

// The phone number can be entered: The phone number or mobile phone number can be (+ 86), (86) +, it can also be a phone number. It can be a district code + A district code-a number.
Function validate_tel (value ){
Reg =/(^ (0 \ D {2, 3} ([-| +])? \ D {7 }$) | (^ (\ D {3, 4} ([-| +])? \ D {7, 8} $) | (^ ([+]? (86 )? [+]?) 1 [3-9] \ D {9} $ )/;
Errorinfo = 'phone number' + value + "Incorrect. Please verify and enter it again! ";
Verifybyreg (value, Reg, errorinfo );
}

// Day: a positive integer greater than 0 and less than or equal to 31
Function validate_day (value ){
Reg =/(^ [0]? [1-9] $) | (^ [1-2] \ D $) | (^ [3] [0-1] $ )/;
Errorinfo = 'date' + value + "error. Please verify and enter it again! ";
Verifybyreg (value, Reg, errorinfo );
}

// Month: Valid month
Function validate_month (value ){
Reg =/(^ [0]? [1-9] $) | (^ [1] [0-2] $ )/;
Errorinfo = 'month' + value + "error. Please verify and enter it again! ";
Verifybyreg (value, Reg, errorinfo );

}

// Year: the valid year is from January 1, 1000 AD to January 1, 2999 AD.
Function validate_year (value ){
Reg =/^ [1-2] \ D {3} $ /;
Errorinfo = 'Year' + value + "error. Please verify and enter it again! ";
Verifybyreg (value, Reg, errorinfo );

}

// Percentage: Valid percentage (digit before 0)
Function validate_percentage (text, value ){
Reg =/(^ [1-9] (\ D )? (\. \ D + )? $) | (^ 0 (\. \ D + )? $) | (^ 100 $ )/;
Errorinfo = text + "" + value + "error. Enter a number ranging from 0 to 100! ";
Verifybyreg (value, Reg, errorinfo );
}

// Coefficient: a positive number less than or equal to 1. Here, the coefficient is followed by two decimal places.
Function validate_modulus (text, value ){
Reg =/^ [0] (\. \ D {2}) $ | ^ [1] (\. [0] {2 })? $ /;
Errorinfo = text + "" + value + "error. Enter a number smaller than 0 to 1! ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify non-negative Positive numbers, including positive integers and floating-point numbers
Function validate_posnumhaszero (text, value ){
Checknull (text, value );
Reg =/^ [1-9] (\ D + )? (\. \ D + )? $ | ^ 0 (\. \ D + )? $ /;
Errorinfo = text + "" + value + "error. Enter a number greater than or equal to 0! ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify non-positive negative numbers, including negative integers and negative floating point numbers
Function validate_negnumhaszero (text, value ){
Checknull (text, value );
Reg =/^ [-] [1-9] (\ D + )? (\. \ D + )? $ | ^ [-] 0 (\. \ D + )? $ /;
Errorinfo = text + "" + value + "error. Enter a number smaller than or equal to 0! ";
Verifybyreg (value, Reg, errorinfo );
}


// Verify positive numbers, including positive integers and floating-point numbers
Function validate_posnum (text, value ){
Checknull (text, value );
Reg =/^ [1-9] (\ D + )? (\. \ D + )? $ | ^ 0 (\. \ D +) $ /;
Errorinfo = text + "" + value + "error. Enter a number greater than 0! ";
Verifybyreg (value, Reg, errorinfo );
}

// Verify the negative number, including the negative integer and the negative floating point number
Function validate_negnum (text, value ){
Checknull (text, value );
Reg =/^ [-] [1-9] (\ D + )? (\. \ D + )? $ | ^ [-] 0 (\. \ D +) $ /;
Errorinfo = text + "" + value + "error. Enter a number smaller than 0! ";
Verifybyreg (value, Reg, errorinfo );
}


// Verification number, including all values, positive, negative, positive, and negative floating point numbers
Function validate_allnum (text, value ){
Checknull (text, value );
Reg =/^ [-| +]? [1-9] (\ D + )? (\. \ D + )? $ | ^ [-| +]? 0 (\. \ D + )? $ /;
Errorinfo = text + "" + value + "error. Enter a number! ";
Verifybyreg (value, Reg, errorinfo );
}

 

Common Regular Expressions

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.