英文:
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 -->
<form action="/action_page.php">
<label for="employee">Employee Name:</label>
<input type="text" id="employee" name="employee" pattern="([0-9]{6})$" title="Last Six Number"><br><br>
<input type="submit">
</form>
<!-- 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.
<input
type="text"
id="employee"
name="employee"
pattern=".*([0-9]{6})$"
title="Last Six Number"
>
JSFiddle: https://jsfiddle.net/ek9gmunh/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论