Home Web Front-end JS Tutorial JS makes verification email/email address regular

JS makes verification email/email address regular

Jun 09, 2018 pm 02:09 PM
Mail

This time I will bring you JS to make a regular verification email/email address. What are the precautions for using JS to make a regular verification email/email address? The following is a practical case, let's take a look.

The rules are defined as follows:

  • Use uppercase letters [A-Z], lowercase letters [a-z], numbers [0-9], underline [_], and minus sign It starts with [-] and a 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:

1

var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

Copy after login

Complete test code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!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('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>

</body>

</html>

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:

1

var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;

Copy after login

Complete test code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!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('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>

</body>

</html>

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:

1

var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|42du.cn)$/;

Copy after login
Complete test code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!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('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>

</body>

</html>

Copy after login
Test result:

1

2

3

4

5

6

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.

The email verification function is now given as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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);

Copy after login
The above isEmail() function lists 11 commonly used email domain names. You can add or delete them as needed.

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:

302 status code in axios

Encapsulate Vue2 routing navigation hook and use it in actual combat

The above is the detailed content of JS makes verification email/email address regular. 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

Video Face Swap

Video Face Swap

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

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)

Is the syntax of adding columns in different database systems the same? Is the syntax of adding columns in different database systems the same? Apr 09, 2025 pm 12:51 PM

The syntax for adding columns in different database systems varies greatly, and varies from database to database. For example: MySQL: ALTER TABLE users ADD COLUMN email VARCHAR(255); PostgreSQL: ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL UNIQUE;Oracle: ALTER TABLE users ADD email VARCHAR2(255);SQL Server: ALTER TABLE users ADD email VARCH

What are the Redis memory data types? What are the Redis memory data types? Apr 10, 2025 pm 02:06 PM

Redis provides five core memory data types: String: basic string storage, supporting incremental/decreasing operations. List: Bidirectional linked list, efficient insertion/deletion operation. Set: Unordered set, used for deduplication operations. Hash: Key-value pair storage, suitable for storing structured data. Zset: Ordered set, each element has fractions, and can be sorted by fractions. Choosing the right data type is critical to optimizing performance.

How to simplify email marketing with Composer: DUWA.io's application practices How to simplify email marketing with Composer: DUWA.io's application practices Apr 18, 2025 am 11:27 AM

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

How to monitor debian mail server How to monitor debian mail server Apr 12, 2025 pm 10:06 PM

To ensure that your Debian mail server runs stably, an effective monitoring mechanism is required. This article introduces several monitoring methods, including log checking, monitoring tools and alarm system settings. 1. Log monitoring The log files of the Debian mail server are usually located in the /var/log/ directory, such as /var/log/mail.log. Regularly checking these logs can help you identify potential problems in a timely manner. 2. Monitoring tools and script examples The following provides several Bash script examples for monitoring CPU, memory and disk space usage and sending email alarms: 1. CPU usage monitoring: #!/bin/bashTHRESHOLD=80EMAILS="your_emai

Nginx virtual host configuration skills, efficiently manage multiple websites Nginx virtual host configuration skills, efficiently manage multiple websites Apr 13, 2025 pm 10:03 PM

Nginx virtual host configuration: Playing around your server garden Have you ever thought about how one server elegantly serves multiple websites at the same time? The answer is the Nginx virtual host configuration. This article will take you into Nginx virtual host configuration tips, allowing you to efficiently manage your "server garden" and avoid some common pitfalls. After reading, you will be able to easily configure the virtual host, understand the mechanism behind it, and write efficient and stable Nginx configuration files. Basic preparation: Don't forget that before starting your toolbox, you need to make sure that Nginx is installed and have some understanding of the basic Linux commands and configuration file structure. We won't explain how to install Nginx here, assuming you've completed this step. remember

How Navicat batch modify text data How Navicat batch modify text data Apr 08, 2025 pm 08:27 PM

Navicat provides tips for batch modifying text data: use SQL statements to perform precise modifications through query generators. Simple text replacement with the help of data import/export. Edit data directly in the Data Grid view for small-scale modifications. Common pitfalls of batch modification: SQL injection risk: filtering and escaping user input. Data type mismatch: Make sure the data type matches. Transaction processing: Use transaction processing to ensure data consistency. Error handling: Use the error handling mechanism and record the error message.

What factors need to be considered for SQL deleting rows What factors need to be considered for SQL deleting rows Apr 09, 2025 pm 12:12 PM

When considering deleting SQL lines, you should be aware of the following: Understand how DELETE statements work and do not confuse them with TRUNCATE or DROP. Use the WHERE clause to specify exactly the rows to be deleted to avoid mistaken deletion. Use batch deletion and transactions as needed to improve efficiency and ensure data consistency. Operate with caution, back up data, and use a test environment to avoid data loss.

How to add columns in SQL at the end of a table? How to add columns in SQL at the end of a table? Apr 09, 2025 pm 01:27 PM

Adding columns at the end of a database table is not easy, the specific operation depends on the database system, table size and data volume. Common errors include: ignoring data types, incorrectly using indexes, and concurrent operations. Optimization strategies include: selecting the appropriate storage engine, using partition tables, and utilizing database replication technology. Good code readability and maintainability also help avoid problems. Only by operating with caution and paying attention to the underlying mechanism can we avoid risks in data security and integrity.

See all articles