Home Web Front-end JS Tutorial JS email mailbox/email address regular filtering implementation

JS email mailbox/email address regular filtering implementation

May 25, 2018 am 09:58 AM
email javascript regular

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.

JS email mailbox/email address regular filtering implementation

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})$/;
Copy after login

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(&#39;cn42du@163.com&#39;) = "+pattern.test(&#39;cn42du@163.com&#39;)+";");
  w("pattern.test(&#39;ifat3@sina.com.cn&#39;) = "+pattern.test(&#39;ifat3@sina.com.cn&#39;)+";");
  w("pattern.test(&#39;ifat3.it@163.com&#39;) = "+pattern.test(&#39;ifat3.it@163.com&#39;)+";");
  w("pattern.test(&#39;ifat3_-.@42du.cn&#39;) = "+pattern.test(&#39;ifat3_-.@42du.cn&#39;)+";");
  w("pattern.test(&#39;ifat3@42du.online&#39;) = "+pattern.test(&#39;ifat3@42du.online&#39;)+";");
  w("pattern.test(&#39;毛三胖@42du.cn&#39;) = "+pattern.test(&#39;毛三胖@42du.cn&#39;)+";");
  function w(val) {
    document.getElementById("main").innerHTML += val +"<br />";
  }
</script>

Copy after login

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})$/;
Copy after login

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(&#39;cn42du@163.com&#39;) = "+pattern.test(&#39;cn42du@163.com&#39;)+";");
  w("pattern.test(&#39;ifat3@sina.com.cn&#39;) = "+pattern.test(&#39;ifat3@sina.com.cn&#39;)+";");
  w("pattern.test(&#39;ifat3.it@163.com&#39;) = "+pattern.test(&#39;ifat3.it@163.com&#39;)+";");
  w("pattern.test(&#39;ifat3_-.@42du.cn&#39;) = "+pattern.test(&#39;ifat3_-.@42du.cn&#39;)+";");
  w("pattern.test(&#39;ifat3@42du.online&#39;) = "+pattern.test(&#39;ifat3@42du.online&#39;)+";");
  w("pattern.test(&#39;毛三胖@42du.cn&#39;) = "+pattern.test(&#39;毛三胖@42du.cn&#39;)+";");
  function w(val) {
    document.getElementById("main").innerHTML += val +"<br />";
  }
</script>

Copy after login

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,

email verification was almost the only condition to ensure the uniqueness of the user. The emergence of temporary mailboxes (also called 10-minute mailboxes or disposable mailboxes) makes the mechanism of mailbox verification and account activation meaningless. The addresses of temporary email addresses are not enumerable. We can only use a whitelist to allow only a limited number of email domain names to pass verification.

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)$/;
Copy after login
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(&#39;cn42du@163.com&#39;) = "+pattern.test(&#39;cn42du@163.com&#39;)+";");
  w("pattern.test(&#39;ifat3@sina.com.cn&#39;) = "+pattern.test(&#39;ifat3@sina.com.cn&#39;)+";");
  w("pattern.test(&#39;ifat3.it@163.com&#39;) = "+pattern.test(&#39;ifat3.it@163.com&#39;)+";");
  w("pattern.test(&#39;ifat3_-.@42du.cn&#39;) = "+pattern.test(&#39;ifat3_-.@42du.cn&#39;)+";");
  w("pattern.test(&#39;ifat3@42du.online&#39;) = "+pattern.test(&#39;ifat3@42du.online&#39;)+";");
  w("pattern.test(&#39;毛三胖dd@42du.cn&#39;) = "+pattern.test(&#39;毛三胖@42du.cn&#39;)+";");
  function w(val) {
    document.getElementById("main").innerHTML += val +"<br />";
  }
</script>

Copy after login
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;
Copy after login
Although scheme 3 verification can ensure security, if the whitelist Too long will cause the pattern

string to be too long. At this time, you can write the email domain name whitelist as an array, use regular expressions for preliminary verification, and use the whitelist for secondary verification of the domain name.

现给出邮箱验证函数如下:

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>
Copy after login

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!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles