【问题标题】:RegEx never matches in a form tag patternRegEx 从不匹配表单标签模式
【发布时间】:2015-04-24 19:05:32
【问题描述】:

练习为 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>

但没有满足所列条件的密码通过。每次我点击提交时,它都会弹出一个提示“请匹配请求的格式”。我做错什么了吗?我一直在阅读正则表达式,对我来说它看起来不错(授予的正则表达式对我来说从来都不是一个强项)

【问题讨论】:

    标签: jquery html regex forms validation


    【解决方案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 相同。 模式不是 被正斜杠包围

    【讨论】:

    • 你太棒了!马上就解决了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多