Home >Web Front-end >JS Tutorial >Detailed explanation of regular expressions for verifying email using js (with code)

Detailed explanation of regular expressions for verifying email using js (with code)

php是最好的语言
php是最好的语言Original
2018-08-06 17:51:549746browse

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 = &#39;test@hotmail.com&#39;; 
document.write(isEmail(str)+&#39;<br />&#39;); 
var str2 = &#39;test@sima.vip.com&#39;; 
document.write(isEmail(str2)+&#39;<br />&#39;); 
var str3 = &#39;te-st@qq.com.cn&#39;; 
document.write(isEmail(str3)+&#39;<br />&#39;); 
var str4 = &#39;te_st@sima.vip.com&#39;; 
document.write(isEmail(str4)+&#39;<br />&#39;); 
var str5 = &#39;te.._st@sima.vip.com&#39;; 
document.write(isEmail(str5)+&#39;<br />&#39;); 
</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(&#39;Mail&#39;).value));" /> 
<p>邮箱不能以 - _ .以及其它特殊字符开头和结束</p> 
<p>邮箱域名结尾为2~5个字母,比如cn、com、name</p>

Related articles:

Detailed explanation of the regular expression js code for authenticating email addresses_regular Expression

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn