Verify how to write the regular expression js code for the mailbox
This article introduces you to verify the mailbox regular expression js code how to write, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The regularity of fuchangxi:
The code is as follows:
/ ^ ([a-zA-Z0-9 percent -]) + @ ([a-zA-Z0-9 percent -]) + (. [a-zA-Z0-9 percent -]) + /
First it has to be one or more word characters or -, plus @, and then one or more word characters or -. And then there's the dot. There can be one or more combinations of and word characters and -.
The code is as follows:
Function isEmail (str) {
Var reg = / ^ ([a-zA-Z0-9 percent -]) + @ ([a-zA-Z0-9 percent -]) + (. [a-zA-Z0-9 percent -]) + /
Return reg.test (str)
}
Var str = 'test@hotmail.com'
[xss_clean] (isEmail (str) +'')
Var str2 = 'test@sima.vip.com'
[xss_clean] (isEmail (str2) +'')
Var str3 = 'te-st@qq.com.cn'
[xss_clean] (isEmail (str3) +'')
Var str4 = 'te_st@sima.vip.com'
[xss_clean] (isEmail (str4) +'')
Var str5 = 'te.._st@sima.vip.com'
[xss_clean] (isEmail (str5) +'')
I don't know much about the specific rules of mailbox. I feel that this regularity is relatively simple.
Count several types of mailbox @ prefixes
1. Pure number
For example: 123456@jb51.net
2. Pure letters
3. Alphanumeric mixing
4. With dots
For example: web.blue@jb51.net
5. Underline
For example: web_blue@jb51.net
6. With connecting wire
For example: web-blue@jb51.net
The mailbox domain has at least one "." And two words, a little more strict, so the final top-level field needs at least 2 letters, the largest? If the domain name "name" prevails, then the maximum is 4, and the leniency point is set to 5 bar ^ _ ^.
Of course, in the above impossible cases: beginning or ending with "_" or "-" and containing special symbols.
Therefore, the regular expression I give is as follows:
^ [A-Za-zd] + ([- _.] [A-Za-zd] +) * @ ([A-Za-zd] + [-.]) + [A-Za-zd] {2jue 5} $
The copy code is as follows:
FChkMail=function (szMail) {
Var szReg=/ ^ [A-Za-zd] + ([- _.] [A-Za-zd] +) * @ ([A-Za-zd] + [-.]) + [A-Za-zd] {2jue 5} $/
Var bChk=szReg.test (szMail)
Return bChk
}