简体   繁体   English

电子邮件正则表达式中缺少验证

[英]Missing validation in email regex

So, here is my code : 所以,这是我的代码:

 <p>A form with a password field that must contain 8 or more characters that are of at least one number, and one uppercase and lowercase letter:</p> <form action=""> Password: <input type="text" name="pw" pattern="[A-Za-z0-9._%+'-\\]+@[a-z0-9.-]+\\.[az]{2,4}$" required title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters"> <input type="submit"> </form> <p><strong>Note:</strong> The pattern attribute of the input tag is not supported in Internet Explorer 9 and earlier versions.</p> 

Why it is allowing 为什么允许

  1. test.com test.com
  2. @text.com @ text.com

I have added @ with a plus to have one or more chars before it. 我在@前面加了一个或多个字符。 but it doesn't seem to work 但它似乎不起作用

You have a problem in your pattern, you are escaping the first ] char : 您的模式有问题,您正在转义第一个] char:

"[A-Za-z0-9._%+'-\]+@[a-z0-9.-]+\.[a-z]{2,4}$"
                 ^
                here

Escape \\ it using another \\ : "[A-Za-z0-9._%+'-\\\\]+@[a-z0-9.-]+\\.[az]{2,4}$" and it should work 使用另一个\\来转义\\"[A-Za-z0-9._%+'-\\\\]+@[a-z0-9.-]+\\.[az]{2,4}$"和它应该工作

ps : You said you cannot use type="email" bu you can see the documentation of it, there is the regex used ps:您说过不能使用type="email" bu,但可以看到它的文档 ,其中使用了正则表达式

The issue is caused by the fact that [A-Za-z0-9._%+'-\\]+@[a-z0-9.-] is a single character class equal to [A-Za-z0-9._%'\\][+@.-] because you escape the ] char ending that [A-Za-z0-9._%+'-] character class prematurely. 此问题是由于以下事实引起的: [A-Za-z0-9._%+'-\\]+@[a-z0-9.-]是等于[A-Za-z0-9._%'\\][+@.-]的单个字符类[A-Za-z0-9._%'\\][+@.-]因为您过早地[A-Za-z0-9._%+'-][A-Za-z0-9._%+'-]字符类的]字符结尾。

Note it is usually enough to use type="email" to check if the input string is email. 请注意,通常通常使用type="email"检查输入的字符串是否为email。 However, since you have some restrictions to use type="text" , you may use 但是,由于使用type="text"有一些限制,因此可以使用

pattern="[\w.%+'-]+@[a-z0-9.-]+\.[a-z]{2,4}"

No \\ should be before ] and you may safely remove $ at the end to only check for the end of string once (it is done automatically by HTML5). 没有\\前应]您可以安全删除$末只有一次(它是由HTML5自动完成)检查字符串的结尾。

The final pattern HTML5 engine will apply to the input will look like ^(?:[\\w.%+'-]+@[a-z0-9.-]+\\.[az]{2,4})$ : HTML5引擎的最终模式将应用于输入,其外观类似于^(?:[\\w.%+'-]+@[a-z0-9.-]+\\.[az]{2,4})$

  • ^(?: - start of string and a non-capturing group ^(?: -字符串开头和非捕获组
  • [\\w.%+'-]+ - 1 or more letters, digits, _ , . [\\w.%+'-]+ - 1个或多个字母,数字, _. , % , + , ' or - %+'-
  • @ - a @ @ -一个@
  • [a-z0-9.-]+ - 1 or molre lowercase ASCII letters, digits, . [a-z0-9.-]+ 1 [a-z0-9.-]+ molre小写ASCII字母,数字, . or - -
  • \\. - a dot -一个点
  • [az]{2,4} - 2, 3 or 4 lowercase ASCII letters [az]{2,4}或4个小写ASCII字母
  • )$ - end of group and string. )$ -组和字符串的结尾。

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

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