This time I will bring you the implementation of JS email mailbox/email address regular filtering. What are the precautions for JS email mailbox/email address regular filtering? The following is a practical case, let’s take a look.
In brief
When doing user registration, the regular expression of the email/mail address is often used. This article lists several options. You can choose the most suitable option according to your project situation.
Option 1 (Commonly used)
The rules are defined as follows:
in capital letters It starts with letters [A-Z], lowercase letters [a-z], numbers [0-9], underscore [_], minus sign [-] and period [.], and needs to be repeated one or more times [ ].
The @ symbol must be included in the middle.
@After that, you need to connect uppercase letters [A-Z], lowercase letters [a-z], numbers [0-9], underscore [_], minus sign [-] and dot [ .], and needs to be repeated one or more times [ ].
must end with a period [.] connecting 2 to 4 digits of uppercase and lowercase letters [A-Za-z]{2,4}.
Use the above rules to give the following regular expression:
var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
Complete test code
nbsp;HTML> <meta> <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title> <p></p> <script> var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; w("pattern.test('cn42du@163.com') = "+pattern.test('cn42du@163.com')+";"); w("pattern.test('ifat3@sina.com.cn') = "+pattern.test('ifat3@sina.com.cn')+";"); w("pattern.test('ifat3.it@163.com') = "+pattern.test('ifat3.it@163.com')+";"); w("pattern.test('ifat3_-.@42du.cn') = "+pattern.test('ifat3_-.@42du.cn')+";"); w("pattern.test('ifat3@42du.online') = "+pattern.test('ifat3@42du.online')+";"); w("pattern.test('毛三胖@42du.cn') = "+pattern.test('毛三胖@42du.cn')+";"); function w(val) { document.getElementById("main").innerHTML += val +"<br />"; } </script>
Test result:
pattern.test('cn42du@163.com') = true;
pattern.test('ifat3@sina.com.cn') = true;
pattern.test('ifat3.it@163. com') = true;
pattern.test('ifat3_-.@42du.cn') = true;
pattern.test('ifat3@42du.online') = false;
pattern.test ('Mao Sanpang@42du.cn') = false;
pattern.test('cn42du@163.com') = true;
pattern.test('ifat3@sina.com.cn') = true;
pattern.test('ifat3.it@163.com') = true;
pattern.test('ifat3_-.@42du.cn') = true;
pattern.test(' ifat3@42du.online') = false;
pattern.test('Mao Sanpang@42du.cn') = false;
Description of Plan 1
Scheme 1 is the most commonly used email regular expression verification scheme and is suitable for most application scenarios. As can be seen from the above test, this expression does not support domain names ending in .online and .store. If you need to be compatible with this type of domain name (more than 4 digits), just adjust the restriction part at the end of the regular expression {2,4} (for example: {2,8}). Another problem is that email usernames cannot include Chinese characters.
Option 2 (revised option 1)
The rules are supplemented as follows:
The user name can include Chinese [\u4e00- \u9fa5]
The end of the domain name can be up to 8 digits{2,8}
The updated regular expression is as follows:
var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;
Complete test code
nbsp;HTML> <meta> <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title> <p></p> <script> var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/; w("pattern.test('cn42du@163.com') = "+pattern.test('cn42du@163.com')+";"); w("pattern.test('ifat3@sina.com.cn') = "+pattern.test('ifat3@sina.com.cn')+";"); w("pattern.test('ifat3.it@163.com') = "+pattern.test('ifat3.it@163.com')+";"); w("pattern.test('ifat3_-.@42du.cn') = "+pattern.test('ifat3_-.@42du.cn')+";"); w("pattern.test('ifat3@42du.online') = "+pattern.test('ifat3@42du.online')+";"); w("pattern.test('毛三胖@42du.cn') = "+pattern.test('毛三胖@42du.cn')+";"); function w(val) { document.getElementById("main").innerHTML += val +"<br />"; } </script>
Test result:
##pattern.test('cn42du@163.com') = true;pattern.test('ifat3@sina.com.cn') = true;
pattern.test('ifat3.it@163.com') = true;
pattern.test('ifat3_-.@ 42du.cn') = true;
pattern.test('ifat3@42du.online') = true;
pattern.test('Mao Sanpang@42du.cn') = true;
Option 3 (Security)
Before the mobile phone verification code appeared, According to the following supplementary rules for option 1: The email domain name can only be 163.com, qq.com or 42du.cn.The regular expression is given as follows:
var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|42du.cn)$/;
nbsp;HTML> <meta> <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title> <p></p> <script> var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|42du.cn)$/; w("pattern.test('cn42du@163.com') = "+pattern.test('cn42du@163.com')+";"); w("pattern.test('ifat3@sina.com.cn') = "+pattern.test('ifat3@sina.com.cn')+";"); w("pattern.test('ifat3.it@163.com') = "+pattern.test('ifat3.it@163.com')+";"); w("pattern.test('ifat3_-.@42du.cn') = "+pattern.test('ifat3_-.@42du.cn')+";"); w("pattern.test('ifat3@42du.online') = "+pattern.test('ifat3@42du.online')+";"); w("pattern.test('毛三胖dd@42du.cn') = "+pattern.test('毛三胖@42du.cn')+";"); function w(val) { document.getElementById("main").innerHTML += val +"<br />"; } </script>
pattern.test('cn42du@163.com') = true; pattern.test('ifat3@sina.com.cn') = false; pattern.test('ifat3.it@163.com') = true; pattern.test('ifat3_-.@42du.cn') = true; pattern.test('ifat3@42du.online') = false; pattern.test('毛三胖dd@42du.cn') = false;
现给出邮箱验证函数如下:
var isEmail = function (val) { var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var domains= ["qq.com","163.com","vip.163.com","263.net","yeah.net","sohu.com","sina.cn","sina.com","eyou.com","gmail.com","hotmail.com","42du.cn"]; if(pattern.test(val)) { var domain = val.substring(val.indexOf("@")+1); for(var i = 0; i<p style="text-align: left;">上述isEmail()函数列举了常用的11种邮箱域名,大家可以根据需要适当补充或删减。</p><p>相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!</p><p>推荐阅读:</p><p><a href="http://www.php.cn/js-tutorial-398128.html" target="_blank">JS数组方法使用步骤详解</a><br></p><p><a href="http://www.php.cn/js-tutorial-398124.html" target="_blank">行内元素padding和margin在什么情况下无效</a><br></p>
The above is the detailed content of JS email mailbox/email address regular filtering implementation. For more information, please follow other related articles on the PHP Chinese website!