为HTML5文本字段设置最后六位数字的正则表达式模式。

huangapple go评论60阅读模式
英文:

Set Last Six Digit Regex Patterns for HTML5 Text field

问题

我需要正则表达式的验证仅检查输入是否在输入中有最后的六位数字。
示例被接受的输入名称如下:

  • Juan Dela Cruz 123456
  • Juan Dela Cruz | 123456
<form action="/action_page.php">
  <label for="employee">员工姓名:</label>
  <input type="text" id="employee" name="employee" pattern="([0-9]{6})$" title="最后六位数字"><br><br>
  <input type="submit">
</form>
英文:

The validation I need for regex is to check only if the input has last six digit on the input.
Sample Accepted Input name are as follows:

  • Juan Dela Cruz 123456
  • Juan Dela Cruz | 123456

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form action=&quot;/action_page.php&quot;&gt;
  &lt;label for=&quot;employee&quot;&gt;Employee Name:&lt;/label&gt;
  &lt;input type=&quot;text&quot; id=&quot;employee&quot; name=&quot;employee&quot; pattern=&quot;([0-9]{6})$&quot; title=&quot;Last Six Number&quot;&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

答案1

得分: 1

尝试使用这个模式:

.*([0-9]{6})$

这样,整个值(包括数字之前的任何内容)都会匹配。在正则表达式中,.* 匹配任何内容。

    type="text"
    id="employee"
    name="employee"
    pattern=".*([0-9]{6})$"
    title="Last Six Number"
>```

JSFiddle链接:https://jsfiddle.net/ek9gmunh/

<details>
<summary>英文:</summary>

Try using this pattern instead:

    .*([0-9]{6})$

That way, the entire value (including anything before the digits) matches.  `.*` in RegEx matches anything.

      &lt;input
        type=&quot;text&quot;
        id=&quot;employee&quot;
        name=&quot;employee&quot;
        pattern=&quot;.*([0-9]{6})$&quot;
        title=&quot;Last Six Number&quot;
      &gt;


JSFiddle:  https://jsfiddle.net/ek9gmunh/

</details>



huangapple
  • 本文由 发表于 2023年7月17日 10:05:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定