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)$/;Complete test code
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>Test result:
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;Although scheme 3 verification can ensure security, if the whitelist Too long will cause the pattern
现给出邮箱验证函数如下:
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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章整理了20+Vue面试题分享给大家,同时附上答案解析。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
