Javascript 印度密码验证正则表达式 - 只有六位数字,不应以“0”开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33865525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indian pincode validation regex - Only six digits, shouldn't start with `0`
提问by Some Java Guy
I have tried Regex accept numeric only. First character can't be 0and What is the regex for "Any positive integer, excluding 0"however that didn't work as per my requirements.
我试过正则表达式只接受数字。第一个字符不能是 0和“任何正整数,不包括 0”的正则表达式是什么,但是这不符合我的要求。
I want exact 6 digit numeric number but shouldn't start with 0
我想要精确的 6 位数字,但不应以 0
I've tried
我试过了
^[1-9][0-9]{6}*$
^([^0][0-9]){6}$
...
Need fine tuning.
需要微调。
回答by Tushar
The problem with ^[1-9][0-9]{6}*$
is it is an invalid regexbecause of {6}*
and ^([^0][0-9]){6}$
is that it is allowing any character that is not 0
followed by sixdigits.
这个问题^[1-9][0-9]{6}*$
是它是一个无效的正则表达式,因为{6}*
和^([^0][0-9]){6}$
是它允许不属于任何字符0
后面6位数字。
Use
用
^[1-9][0-9]{5}$
Explanation:
解释:
^
: Starts with anchor[1-9]
: Matches exactly one digit from 1 to 9[0-9]{5}
: Matches exactly five digits in the inclusive range0-9
$
: Ends with anchor
^
: 以锚点开头[1-9]
: 匹配从 1 到 9 的一位数字[0-9]{5}
: 正好匹配包含范围内的五位数字0-9
$
: 以锚点结尾
HTML5 Demo:
HTML5 演示:
input:invalid {
color: red;
}
<input type="text" pattern="[1-9][0-9]{5}" />
回答by navule
Some websites and banks have the habit of spacing pincode after 3 digits. To match both 515411 and 515 411 the following pattern will help.
一些网站和银行有在 3 位数字后间隔 PIN 码的习惯。要同时匹配 515411 和 515 411,以下模式会有所帮助。
^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$
- ^[1-9]{1} - PIN Code that starts with digits 1-9
- [0-9]{2} - Next two digits may range from 0-9
- \s{0,1} - Space that can occur once or never
- [0-9]{3}$ - Last 3 needs to be digits ranging from 0-9
- ^[1-9]{1} - 以数字 1-9 开头的 PIN 码
- [0-9]{2} - 下两位数字的范围可能是 0-9
- \s{0,1} - 可以出现一次或从不出现的空间
- [0-9]{3}$ - 最后 3 位需要是 0-9 之间的数字
回答by Sumedh Deshpande
This regular expression covers;
这个正则表达式涵盖;
PIN code doesn't start from zero
Allows 6 digits only
Allows 6 consecutive digits (e.g. 431602)
Allows 1 space after 3 digits (e.g. 431 602)
PIN 码不是从零开始的
只允许 6 位数字
允许 6 个连续数字(例如 431602)
3 位数字后允许 1 个空格(例如 431 602)
([1-9]{1}[0-9]{5}|[1-9]{1}[0-9]{3}\s[0-9]{3})