simply says

often uses regular expressions for mailbox / mail addresses when they are registered for users. This article enumerates several schemes, and we can choose the most suitable scheme according to the situation of the project.

Email regular expression

(common) scheme 1 rules are defined as follows:

  • [A-Z] in uppercase and lowercase letters [a-z], digital [0-9], [_], minus will decline line and the dot [.] at the beginning, and need to repeat several times to [+]. The
  • must include the @ symbol.
  • @ need to connect the uppercase letters and lowercase letters [A-Z], [0-9], [a-z] digital [_], minus will decline line and the dot [.], and need to repeat several times to [+].
  • must end with 2 to 4 point [.] connecting the letters [A-Za-z]{2,4}.

using the above rules are as follows:

 var regular expression 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> expression and analysis of regular mail / email address (JavaScript, email, regex) < /title> < /head> < body> < div id= "main" > < /div> < 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 (three' gross fat @42du.cn') = +pattern.test (three'. Fat + @42du.cn')"; "); function w (VAL) {document.getElementById (" main ").InnerHTML = Val + < br /> /script> <}; < /body> < /html > 

pattern.test test results: ('cn42du@163.com')
(pattern.test = true;'ifat3@sina.com.cn')
= true; pattern.test = true;
('ifat3.it@163.com') pattern.test ('ifat3_-.@42du.cn') = true; br> < pattern.test ('ifat3@42du.online') = false
; pattern.test (three 'gross fat @42du.cn') = false ('c;
pattern.test N42du@163.com'= true);
pattern.test ('ifat3@sina.com.cn') = true;
= true; pattern.test ('ifat3.it@163.com')
pattern.test ('ifat3_-.@42du.cn') = true;
= false; pattern.test ('ifat3@42du.online')
pattern.test (three' gross fat @42du.cn') = false;

program 1 Program 1 is the mail regular expression validation the most common solution, but also suitable for most application scenarios. As you can see from the above, the expression does not support the domain name at the end of.Online and.Store. If you need this kind of compatible domain name (more than 4 bits), adjust the regular end {2,4} can be limited ({2,8}). Another problem is that the mail username does not include Chinese.

2 (revised proposal 1)

  • rules are as follows: the user can include Chinese [u4e00-u9fa5]
  • domain name end of the longest regular expressions are as follows 8 {2,8}
  • updated:
 var pattern = / ^ ([A-Za-z0-9_-.u4e00-u9fa5]) +@ ([A-Za-z0-9_-.]) +. ([A-Za-z]{2,8}) / 

; test code

 < DOCTYPE HTML> <!; html> < head> < meta charset= "UTF-8" > < title> expression and analysis of regular mail / email address (JavaScript, email, regex) < /title> < /head> < body> < div id= main > < /div> < script> VAR = pattern / ^ ([A-Za-z0-9_-.u4e00-u9fa5]) +@ (A-Za-z0-9_-.]) +. ([A-Za-z]{2,8}) / w (pattern.tes; T ('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 (three 'gross fat @42du.cn') = +pattern.test (@42du.cn'+ three' gross fat)"; "); function w (VAL) {document.getElementById (" ".InnerHTML = Val + main) < br /> < /script>}";; < /body> < /html > 

pattern.test test results: ('cn42du@163.com') = true;
= true; pattern.test ('ifat3@sina.com.cn')
pat Tern.test ('ifat3.it@163.com') = true
('ifat3_-.@42du.cn'); pattern.test = true;
= true; pattern.test ('ifat3@42du.online')
pattern.test (three' gross fat @42du.cn') = true;

3 (safety) before the

in mobile phone verification code, verification is to ensure that the only condition about the mailbox user uniqueness. The emergence of a temporary mailbox (also known as a 10 minute mailbox or a one-time mailbox) makes the mailbox verification and account activation a loss of meaning. The temporary email address is nonenumerative, we can only take a whitelist approach, allows only limited by verifying the mailbox name.

complements the following rules according to program 1: the

mailbox domain name can only be 163.com, qq.com, or 42du.cn.
is a regular expression 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> expression and analysis of regular mail / email address (JavaScript, email, regex) < /title> < /head> < body> < div id= "main" > < /div> < 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 (three 'gross fat dd@42du.cn') = +pattern.test (@42du.cn'+ three' gross fat) ";" function w (VAL);) {document.getElementById ("main").InnerHTML = Val + < br /> < /script>} ";; < /body> < /html> 

 pattern.test test results: ('cn42du@ 163.com') = true; pattern.test = false; pattern.test ('ifat3@sina.com.cn') ('ifat3.it@163.com') (pattern.test = true;'ifat3_-.@42du.cn') = true; pattern.test = false; ('ifat3@42du.online') pattern.test (three' gross fat dd@42du.cn'= false; 

) scheme 3 verification can ensure the safety, but if the white list is too long will cause the pattern The string is too long. At this time, you can write the white list of the mailbox's domain name into an array, use regular expressions to do the preliminary verification, and use the white list to do the two verification of the domain name.

is given to verify the function as follows:

 mailbox 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 output (

(isEmail) the function enumerates 11 kinds of commonly used mail domain, we can according to the need of appropriate to add or delete.

above is three fat for the understanding and analysis of mailbox regular expressions, if there is a shortage, please correct them.

This paper fixed link:http://www.script-home.com/regular-expression-and-analysis-of-javascript-email-mailbox-mail-address.html | Script Home | +Copy Link

Article reprint please specify:Regular expression and analysis of JavaScript email mailbox / mail address | Script Home

You may also be interested in these articles!