javascript 是否验证 3 位数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4947785/
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
Validate 3 digit number or not
提问by Vinoth13
can anyone tell me the regular expression to check whether the entered number is 3 digit number or not... it should not allow alphabets also....
谁能告诉我正则表达式来检查输入的数字是否是 3 位数字......它也不应该允许字母......
回答by Guillaume86
^\d{3}$
and more chars to pass my answer
和更多字符来传递我的答案
回答by Jamiec
Regex for 3 numbers would be ^[0-9]{3}$or ^\d{3}$
3 个数字的正则表达式将是^[0-9]{3}$或^\d{3}$
回答by David
Sure, Here you go. ^[0-9]{3}$ OR ^\d{3}$
喏,给你。^[0-9]{3}$ 或 ^\d{3}$
The "^" and "$" at the beginning and the end of each specify that the string being compared must be exactly 3 digits. if you omit those, then dsaasd888adsad would be a match because \d{3} is being matched SOMEWHERE in the string.
每个开头和结尾的“^”和“$”指定被比较的字符串必须正好是 3 位数字。如果您省略这些,那么 dsaasd888adsad 将是一个匹配项,因为 \d{3} 在字符串中的某处匹配。
Or you could google "regex 3 digit number".
或者你可以谷歌“regex 3 digit number”。
Personally, I always go here for regex examples: http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
就个人而言,我总是去这里查看正则表达式示例:http://regexlib.com/DisplayPatterns.aspx?cattabindex=2& categoryId=3
回答by Surreal Dreams
\d\d\dor \d{3}should work for a 3 digit number. This won't allow any punctuation, letters, symbols, etc - it's specifically 3 numerals. If you need to allow for a decimal point, that's going to be a little different.
\d\d\d或者\d{3}应该适用于 3 位数字。这将不允许任何标点符号、字母、符号等 - 它特别是 3 个数字。如果您需要允许小数点,那会有些不同。

