【问题标题】:HTML field validation, pattern attributeHTML 字段验证,模式属性
【发布时间】:2017-10-28 10:32:25
【问题描述】:

我不知道粗体部分是什么意思:

&lt;input name="zip_code" type="text" pattern=<strong>"\d{5}(-\d{4})?"</strong> required="required" /&gt;

谁能帮帮我?

【问题讨论】:

标签: html validation design-patterns zipcode


【解决方案1】:

\d{5} 表示需要 5 位数字,并且 (-\d{4})? 表示字符“-”和其他4个数字是可选的

【讨论】:

    【解决方案2】:
    pattern="\d{5}(-\d{4})?"
    

    意思

    1. 以 5 位数字开头
    2. 后跟一个破折号(可选)
    3. 后跟 4 位数字(可选)

    例子

    48412 - Accepted
    4488 - Rejected
    44775-4412 - Accepted
    74547-45745 - Rejected
    

    规则如下

    ^       a string that starts with...
    (       either
      \d    a digit (0-9)...
      {4}   that repeats four times...
    |       or
      \d    a digit (0-9)...
      {6}   that repeats six times...
    )
    $ and then ends
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2019-09-06
      • 2011-02-11
      • 2016-01-24
      • 2019-09-14
      • 2011-11-14
      • 1970-01-01
      相关资源
      最近更新 更多