首页 > 解决方案 > 电子邮件正则表达式中缺少验证

问题描述

所以,这是我的代码:

<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.-]+\.[a-z]{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>

为什么它允许

  1. 测试网
  2. @text.com

我加@了一个加号,在它之前有一个或多个字符。但它似乎不起作用

标签: javascriptregexvalidation

解决方案


您的模式有问题,您正在转义第一个]char :

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

\使用另一个转义它\: "[A-Za-z0-9._%+'-\\]+@[a-z0-9.-]+\.[a-z]{2,4}$"它应该可以工作

ps:你说你不能使用type="email"bu 你可以看到它的文档,有使用的正则表达式


推荐阅读