Home > Article > Web Front-end > Detailed explanation of regular expressions for verifying email using js (with code)
The most authentic regular statement of mailbox regex is ^[a-z0-9] ([._\\-]*[a-z0-9])*@([a-z0-9] [-a -z0-9]*[a-z0-9] .){1,63}[a-z0-9] $ Let’s explain the regular rules of
fuchangxi:
/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
It must start with one or more word characters or -, plus @, and then one or more word characters or -. Then there is a combination of dot "." and word characters and -. There can be one or more combinations.
<script type="text/javascript"> function isEmail(str){ var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; return reg.test(str); } var str = 'test@hotmail.com'; document.write(isEmail(str)+'<br />'); var str2 = 'test@sima.vip.com'; document.write(isEmail(str2)+'<br />'); var str3 = 'te-st@qq.com.cn'; document.write(isEmail(str3)+'<br />'); var str4 = 'te_st@sima.vip.com'; document.write(isEmail(str4)+'<br />'); var str5 = 'te.._st@sima.vip.com'; document.write(isEmail(str5)+'<br />'); </script>
I don’t know much about the specific rules of email. I feel that this regular rule is relatively simple
Count the types of email @ prefixes
1. Pure numbers
For example: 123456@jb51.net
2. Pure letters
3. Mixed letters and numbers
4. Dotted
For example: web.blue@jb51.net
5. Underlined
For example: web_blue@jb51.net
6. With connecting lines
For example: web-blue@jb51.net
The email domain must have at least one "." and two words. To be stricter, the final top-level domain must be at least 2 letters. What is the maximum? Based on the domain name "name", the maximum is 4. If it is more relaxed, let's set it to 5^_^.
Of course the above is impossible: starting or ending with "_" or "-" and containing special symbols.
Therefore, the regular expression I gave is as follows:
^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$
<script type="text/javascript"> fChkMail=function(szMail){ var szReg=/^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/; var bChk=szReg.test(szMail); return bChk; } </script> <input type="text" id="Mail" value="" /> <input type="button" value="验证邮箱地址" onclick="alert(fChkMail(document.getElementById('Mail').value));" /> <p>邮箱不能以 - _ .以及其它特殊字符开头和结束</p> <p>邮箱域名结尾为2~5个字母,比如cn、com、name</p>
Related articles:
Regular expression for verifying email
Related videos:
JavaScript Regular Expression Video Tutorial
The above is the detailed content of Detailed explanation of regular expressions for verifying email using js (with code). For more information, please follow other related articles on the PHP Chinese website!