There is a php regular expression mailbox problem
The code is as follows: |
Copy code |
$ A = '/([w. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4 })/'; $ B = '1412316545645454545454545k @ qq.com '; If (preg_match ($ a, $ B )){ Echo "the email is valid "; } Else { Echo "illegal email "; } |
Why is the above output valid ??? {} Isn't it only allowed to put 2 to 10 digits here? It already exceeded 10 digits
This regular expression can be matched, matching this part '545454545k @ qq.com'
The first part of the 1412424545645454 does not match, so it is still valid.
If you want to match the entire segment correctly, you can change it to this
The code is as follows: |
Copy code |
$ A = '/^ ([w. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4}) $ /'; |
Pay attention to your regular expression
The code is as follows: |
Copy code |
/([W. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4 })/
|
He has no assertions, meaning that the string matches as long as there are matching parts
Therefore, {} can only match 2 to 10 characters, but does not match the entire string.
You must use assertions to determine whether a full string is matched. For example, ^ $
The code is as follows: |
Copy code |
/^ ([W. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4}) $/
|
Note that the header and tail are added with ^ $
The code is as follows: |
Copy code |
$ A = '/^ ([w. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4}) $ /'; $ B = '1412316545645454545454545k @ qq.com '; If (preg_match ($ a, $ B )){ Echo "the email is valid "; } Else { Echo "illegal email "; } |
Now let's make better writing.
PHP mail verification regular expression newbie instance:
The code is as follows: |
Copy code |
<? Php Function isEmail ($ email ){ If (preg_match ("/^ [0-9a-zA-Z] + @ ([0-9a-zA-Z] +) [.]) + [a-z] {2, 4} $/I ", $ email )){ Return 'yesmail '; } Else { Return 'not mailbox '; } } ?> Regular expression for Javascript (js) email verification: Myreg =/^ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] +. [a-zA-Z] {2, 4} $ /; This can be verified in the form of a mailbox such as: I @julying.com, I @ I .com Javascript (js) email address verification regular expression newbie instance: <Script type = "text/javascript"> Function isEmail (val ){ Var myreg =/^ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] +. [a-zA-Z] {2, 4} $ /; If (! Myreg. test (val )) Return 'not mailbox '; Return 'yesmail '; }; Alert (isEmail ('I @ julying.com ')); </Script> |