It seems that it is okay to verify a mailbox like sofish@163.com. However, for a closer look AT the non-capturing reference behind AT (@), the usage is * (any occurrence)
First look at the following code:
FunctionisValidMail (sText) {varreMail =/^ (? : \ W + \.?) * \ W + @(? : \ W + \.?) * \ W + $/; returnreMail. test (sText );} |
It seems that it is okay to verify a mailbox like sofish@163.com. However, for a closer look AT the non-capturing reference behind AT (@), the usage is * (any occurrence ):
VarreMail =/^ (? : \ W + \.?) * \ W + @(? : \ W + \.?) * \ W + $ /; |
This also includes 0 occurrences. A mailbox like sofish @ 163com is also verified. Obviously, "." must appear at least once. Therefore, "+" indicates at least once. But here, after AT, we can write an end like 163.com.cn, but directly change it to "+", so that 163..com.cn can pass verification. The following is my method:
FunctionisValidMail (sText) {varreMail =/^ (? : \ W + \.?) * \ W + @(? : \ W + \.) + \ w + $/; alert (reMail. test (sText ))} |
The rule "." appears only once. Then, after a non-capturing reference, make the rest display at least once and end with any character. However, here the "\ w" is underlined, that is to say, mail like sofish@163_.com _ may also be difficult to pass, apparently, this is an invalid email, ". you can only use English letters (at least I have never seen a domain name followed by a number ). In addition, it should be noted that "\ w" represents the characters including underscores, and does not need to be abbreviated as follows:
Therefore, the code above can be modified as follows:
FunctionisValidMail (sText) {varreMail =/^ (? : [A-z \ d] + [_ \-\ + \.]?) * [A-z \ d] + @(? :( [A-z \ d] + \-?) * [A-z \ d] + \.) + ([a-z] {2,}) + $/I; alert (reMail. test (sText ))} |
In fact, you can write it like this, right. To be more thorough, you can also consider not using the same letter after each point AT, such as .com.cn.com.cn (obviously, such domain names still exist, and I am wrong, thanks to @ StonyWang for reminding me, is there anyone doing this ?) This is not allowed. You can consider using capture references for storage and comparison and verification. Think of it as an exercise question (it suddenly seems like a math question in high school ).
The instance code is as follows:
Test: Use a Javascript Regular Expression to verify the Email address Enter the Email address and click test. The error address will return false... correct, displayed? Guess by yourself: Var sText = document. getElementById ('mail '). value; document. getElementById ('mail '). onblur = function sValue () {sText = this. value;} function isValidMail (sText) {var reMail =/^ (? : [A-zA-Z0-9] + [_ \-\ + \.]?) * [A-zA-Z0-9] + @(? :( [A-zA-Z0-9] + [_ \-]?) * [A-zA-Z0-9] + \.) + ([a-zA-Z] {2,}) + $/; alert (reMail. test (sText)} script |