简体   繁体   English

用于 Redux 表单中电子邮件验证的正则表达式

[英]Regex for email validation in Redux form

I'm trying to build a regex for email validation, i have additional several conditions to check:我正在尝试构建一个用于电子邮件验证的正则表达式,我还有几个条件要检查:

  1. allowed: uppercase and lowercase English letters, digits 0 to 9允许:大写和小写英文字母,数字0到9

  2. allowed: "_", "-", “.”, “@” and "'";允许:“_”、“-”、“.”、“@”和“'”;

3.a. 3.a. Character "."特点 ”。” is allowed to be provided if it is not the first or last character如果不是第一个或最后一个字符,则允许提供

3.b. 3.b. "." “。” does not appear two or more times consecutively不会连续出现两次或多次

3.c. 3.c. "." “。” must appear at least one time in domain name必须在域名中至少出现一次

  1. should not contain “.@” or “@.”不应包含“.@”或“@”。

  2. should not start with "."不应以“.”开头

  3. “@” must appear once “@”必须出现一次

  4. In the domain name, the string length, after last “.”在域名中,字符串长度,在最后一个“.”之后。 , should have at least 2 characters , 应该至少有 2 个字符

  5. Leading "_" is not allowed in the domain name域名中不允许以“_”开头

I have created the following regex:我创建了以下正则表达式:

^[a-zA-Z0-9_'-]{1}[a-zA-Z0-9._'-]*([^.]@[^._])([a-zA-Z0-9_.'-])+[.]{1}[a-zA-Z0-9_'-]{2,}$ 

I'ts covered all the sections exclude section 3.b.我没有涵盖除第 3.b 节之外的所有部分。

example for valid email: ya.ll.tj@gg.cc example for invalid email: ya..lf@dd.cc , yssss...@kk.dd有效电子邮件示例: ya.ll.tj@gg.cc 无效电子邮件示例: ya..lf@dd.cc , yssss...@kk.dd

Thank's谢谢

At the beginning of the pattern, negative lookahead for .*\\.{2} to ensure that there are never two .在模式的开头,对.*\\.{2}进行负前瞻以确保永远不会有两个. s in a row:连续:

(?!.*\.{2})

There are also some fixes and optimizations to make.还有一些修复和优化。 By using negative lookahead for (a single) .通过对 (a single) 使用负前瞻. at the start of the string, you can keep from having to repeat the character set twice (since the first is identical to the second, only without a . ).在字符串的开头,您可以避免重复两次字符集(因为第一个与第二个相同,只是没有. )。

Negative character sets alone can match any character not in the set - for example, your [^.] right before @ can match a newline character, which surely isn't desirable.单独的负字符集可以匹配不在集合中的任何字符 - 例如,在@之前的[^.]可以匹配换行符,这肯定是不可取的。 Instead, to ensure that the last character before the @ isn't a .相反,要确保@之前的最后一个字符不是. , use another character set: , 使用另一个字符集:

^(?!.*\.{2})(?!\.)[a-z0-9_.'-]*[a-z0-9_'-]@

(in modern environments, you could negative lookbehind for . at the @ instead, similar to the first technique, to avoid having to repeat the similar character set, but JS lookbehind isn't supported everywhere yet) (在现代环境中,您可以在@处对.进行否定回溯,类似于第一种技术,以避免重复类似的字符集,但 JS 回溯尚未在任何地方都得到支持)

Use the case-insensitive flag as well, to avoid having to use [a-zA-Z everywhere.也使用不区分大小写的标志,以避免必须在任何地方使用[a-zA-Z In full:在全:

^(?!.*\.{2})(?!\.)[a-z0-9_.'-]*[a-z0-9_'-]@(?!_)(?:[a-z0-9_'-]+\.)+[a-z0-9_'-]{2,}$

https://regex101.com/r/tZ7LHt/2 https://regex101.com/r/tZ7LHt/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM