search
HomeWeb Front-endJS TutorialUse regular expressions to filter email addresses/email addresses

This time I will bring you the regular expressionsto filter email mailboxes/email addresses and the precautionsto use regular expressions to filter email mailboxes/email addresses. The following are Let’s take a look at practical cases.

When doing user registration, regular expressions for email/email addresses are often used. This article lists several solutions. You can choose the most suitable solution according to your project situation

In short

When registering users, email is often used /Regular expression for email address. This article lists several options. You can choose the most suitable option according to your project situation.

Use regular expressions to filter email addresses/email addresses

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

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title>
</head>
<body>
<p id="main"></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>
</body>
</html>

Test result:

pattern.test(&#39;cn42du@163.com&#39;) = true;
pattern.test(&#39;ifat3@sina.com.cn&#39;) = true;
pattern.test(&#39;ifat3.it@163.com&#39;) = true;
pattern.test(&#39;ifat3_-.@42du.cn&#39;) = true;
pattern.test(&#39;ifat3@42du.online&#39;) = false;
pattern.test(&#39;毛三胖@42du.cn&#39;) = false;
pattern.test(&#39;cn42du@163.com&#39;) = true;
pattern.test(&#39;ifat3@sina.com.cn&#39;) = true;
pattern.test(&#39;ifat3.it@163.com&#39;) = true;
pattern.test(&#39;ifat3_-.@42du.cn&#39;) = true;
pattern.test(&#39;ifat3@42du.online&#39;) = false;
pattern.test(&#39;毛三胖@42du.cn&#39;) = false;

Description of Scheme 1

Scheme 1 is the most commonly used email regular expression verification scheme and is also 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

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title>
</head>
<body>
<p id="main"></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>
</body>
</html>

Test result:

pattern.test(&#39;cn42du@163.com&#39;) = true;
pattern.test(&#39;ifat3@sina.com.cn&#39;) = true;
pattern.test(&#39;ifat3.it@163.com&#39;) = true;
pattern.test(&#39;ifat3_-.@42du.cn&#39;) = true;
pattern.test(&#39;ifat3@42du.online&#39;) = true;
pattern.test(&#39;毛三胖@42du.cn&#39;) = true;

Option 3 (safety)

On mobile phone Before the 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 in 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

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title>
</head>
<body>
<p id="main"></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>
</body>
</html>

Test result:

pattern.test(&#39;cn42du@163.com&#39;) = true;
pattern.test(&#39;ifat3@sina.com.cn&#39;) = false;
pattern.test(&#39;ifat3.it@163.com&#39;) = true;
pattern.test(&#39;ifat3_-.@42du.cn&#39;) = true;
pattern.test(&#39;ifat3@42du.online&#39;) = false;
pattern.test(&#39;毛三胖dd@42du.cn&#39;) = false;

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.

The email verification function is now given as follows:

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< domains.length; i++) {
      if(domain == domains[i]) {
        return true;
      }
    }
  }
  return false;
}
// 输出 true
isEmail(cn42du@163.com);

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use Vue.js custom events to perform form input components

Vue uses the following table to modify the array What to do when the page does not render

The above is the detailed content of Use regular expressions to filter email addresses/email addresses. 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
JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

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