-- Quick literacy with regular expressions --
\ As a conversion, that is, the characters after "\" do not follow the Original Meaning
^ Matches the beginning of an input or a line,/^ A/matches "An a", but does not match "an"
$ Matches the end of An input or line,/a $/matches "an a", but does not match "An"
* Match the first metacharacters 0 or multiple times (any number of times),/a */match zero or multiple
+ Match the first metacharacters once or multiple times./a +/will match one or more a,/ba */will match ba, baa, baaa
? Match the first metacharacters 0 or 1 times,/ba */will match B, ba
X | y matches x or y
(X) Match x and save x in the variable $1... $9.
{N} exact match n times
{N,} match more than n times
{N, m} matches n-m times
[Xyz] character set (character set) that matches any one of the set's characters (or metacharacters)
[^ Xyz] does not match any character in this set
[\ B] matches a return character.
\ B matches the boundary of a word
\ B matches the non-boundary of a word
\ CX here, X is a control character, // \ cM/matches Ctrl-M
\ D matches a numeric character, which is equivalent to [0-9]
\ D matches a non-numeric character, which is equivalent to [^ 0-9]
\ N matches a linefeed.
\ R matches a carriage return.
\ S matches a blank character, including \ n, \ r, \ f, \ t, \ v, etc.
\ S matches a non-blank character, which is equivalent to [^ \ n \ f \ r \ t \ v]
\ T matches a tab
\ V matches a straight-heavy Tab
\ W number, letter, underline, equivalent to [a-zA-Z0-9 _]
\ W non \ w, equivalent to [^ a-zA-Z0-9 _]
-- Quick introduction to regular expressions --
Well, after getting familiar with the above content, we may not be able to use regular expressions freely. Below we use regular expressions to implement form verification, so that we can get started quickly.
First, we will introduce the regular expressions that are frequently used in Form Verification:
Verify zip code:/^ \ d {6} $/
Regular Expressions start with/^ and end with $/. They represent matching the beginning and end of a row respectively. \ d {6} indicates exact matching of six integers.
Verify the landline phone:/^ \ d {3}-\ d {8} | d {4}-\ d {7} $/
Verification ID number:/^ \ d {15 }$ | ^ \ d {18 }$ | ^ \ d {17} [Xx] $/
The red part accurately matches the 15-digit integer with the ID number of the previous 15-digit province.
The blue part indicates exact 18-digit integer matching the current 18-digit province ID.
The pink part indicates exact match of 17 integers. the last digit matches X or x. the last digit of the corresponding provincial certificate is the 18-digit provincial Certificate of X or x.
The three parts are the or relationship.
Verify email address:/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $/
Since this seems a bit dizzy, we can directly go to the application.
-- Quick application of regular expressions --
1. Password inspection requirements: The password must contain more than eight characters and must contain letters and numbers.
Function checkPassWord ()
{
Var passWord = document. getElementById ("tfPassWord"). value;
Var passWordO =/^ [a-zA-Z0-9] * [^ a-zA-Z0-9] + [a-zA-Z0-9] * $ /;
If (passWord. search (passWordO) =-1)
{
Alert ("the password format you entered is incorrect. Please enter it again! ");
Return false;
}
If (document. getElementById ("tfPassWord"). length <= 8)
{
Alert ("the password you entered is less than 8 digits. Please enter a password greater than 8 digits! ");
Return false;
}
Return true;
}
2. Verification Test requirements for the date of birth: Format: yyyy-mm-dd
Function checkBirthday ()
{
Var birthday = document. getElementById ("tfBirthday"). value;
Var birthdayO =/^ \ d {4}-\ d {2}-\ d {2} $ /;
If (birthday. search (birthdayO) =-1)
{
Alert ("the date of birth you entered is incorrect. Please try again! ");
Return false;
}
Return true;
}
3. ID card number verification call event: onblur = "checkID ()"
Function checkID ()
{
Var ID = document. getElementById ("tfID"). value;
Var IDO =/^ (\ d {15} | \ d {18} | \ d {17} [Xx]) $ /;
If (ID. search (IDO) =-1)
{
Alert ("the ID number you entered is invalid! ");
Return false;
}
Var year = ID. substr (6, 4 );
Var mm = ID. substr (10, 2 );
Var dd = ID. substr (12, 2 );
Var birthdate = year + "-" + mm + "-" + dd;
If (document. getElementById ("tfBirthday"). value. search (birthdate) =-1)
{
Alert ("enter a valid ID card number! ");
Return false
}
Return true;
}
4. Zip code verification requirements: the zip code must be 6 digits
Function checkPostcode ()
{
Var postcode = document. getElementById ("tfPostcode"). value;
Var postcodeO =/^ \ d {6} $ /;
If (postcode. search (postcodeO) =-1)
{
Alert ("enter the correct zip code! ");
Return false;
}
Return true;
}
5. email verification requirements: the normal email address is met.
Function checkEmail ()
{
Var email = document. getElementById ("tfEmail"). value;
Var emailO =/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /;
If (email. search (emailO) =-1)
{
Alert ("the email address you entered is incorrect. Please enter it again! ");
Return false;
}
Return true;
}
Author: "extreme scholar"