RegEx永远不会匹配表单标记模式

时间:2015-04-24 19:05:32

标签: jquery html regex forms validation

练习制作和使用jQuery类的表单。作为任务的一部分,我们获得了以下内容:

    Password must follow the pattern:
    - first character must be a letter
    - following characters may be letters, numbers, and an underscore
    - must be between 3 and 15 total characters
the password and the confirm password entries must match

You may use HTML5 input types and attributes. The following regular     
expression can be used for the password: /^[a-zA-Z]\w{3,14}$/  

所以,在我的页面中,我把密码标签(在表单标签内)如下:

<input type="text" id="password" pattern="/^[a-zA-Z]\w{3,14}$/" required>

然而,没有符合所列标准的密码通过。每次我点击提交时,都会给我一个弹出式提示&#34;请匹配所要求的格式&#34;。我做错什么了吗?我一直在阅读正则表达式,它看起来对我好(授予正则表达式从来就不是我的强项)

1 个答案:

答案 0 :(得分:1)

删除正则表达式文字分隔符以及pattern属性中的开始和结束标记:

<input type="text" id="password" pattern="[a-zA-Z]\w{2,14}" required>

(我还将3更改为2,因为字符总数应该介于3到15之间。

来自the MDN

  

<强>图案

     

检查控件值的正则表达式。的的   模式必须匹配整个值,而不仅仅是某个子集。使用   title属性用于描述帮助用户的模式。这个   当type属性的值为text时,属性适用   搜索,电话,网址或电子邮件;否则会被忽略。经常   表达式语言与JavaScript相同。 模式不是   被正斜杠包围