javascript 如何验证具有正则表达式和一些可接受值的文本字段的值“0”或“00”或“000”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4587967/
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
How to validate for Values "0" or "00" or "000" for textfield which has the regex and some accepted values
提问by Someone
var myregex = /^[0-9][0-9]{0,3}$|^[0-9][0-9]{0,3}[\.][0-9]$/;
if(!myregex.test($('#txt1').val()) || $('#txt1').val()=="0"){
alert("Please Enter Correct value");
}
Now My mandatory things are it should
现在我必须做的事情是它应该
- follow Regex
- we should not able to enter the value "0" or "00" or "000"
- 遵循正则表达式
- 我们不应该输入值“0”或“00”或“000”
So my above if() raises an alert if the user enter a "0" and if we enter "00" or "000" or "0000" it doesnot catch it .But if we enter "00000" it catches it due to regex. How can i handle for 2,3,4 zeros .Now Here I can put a regex which doesnot accept zeros such as this one "/^[^0].*$/" but My textfield should Accepts this values as right (0.2,0.3,0.4) .so i cannot use this regex.
所以我上面的 if() 会在用户输入“0”时发出警报,如果我们输入“00”或“000”或“0000”,它不会捕获它。但是如果我们输入“00000”,它会因正则表达式而捕获它. 我如何处理 2,3,4 个零。现在我可以在此处放置一个不接受零的正则表达式,例如“ /^[^0].*$/”,但我的文本字段应正确接受此值(0.2,0.3,0.4)。所以我不能使用这个正则表达式。
回答by ken
Avoid regex, it's a nightmare to get exactly right, and to maintain in the future. Instead, use built-in JS string parsing functionality:
避免使用正则表达式,这是一场完全正确的噩梦,并在未来保持。相反,使用内置的 JS 字符串解析功能:
var val = parseFloat($('#txt1').val());
if (isNaN(val) || (val === 0))
{
alert("Please Enter Correct value");
}
回答by Alan Moore
/^[1-9]\d{0,3}(?:\.\d)?$|^0\.[1-9]$/
The first alternative matches any number from 1.0to 9999.9. The first digit has to be at least 1, which eliminates leading zeroes. The fraction part is optional ((?:\.\d)?), so it also matches integers from 1to 9999. The second alternative handles the special cases of 0.1through 0.9.
第一个选项匹配从1.0到的任何数字9999.9。第一个数字必须至少为1,这样可以消除前导零。分数部分是可选的 ( (?:\.\d)?),因此它也匹配从1to 的整数9999。第二种选择处理0.1through的特殊情况0.9。
回答by Alex Vidal
It looks like your regular expression requires 0 through 9999, or 0.0 through 9999.9, but you do not want to accept a 0 alone, or a series of zeros.
看起来您的正则表达式需要 0 到 9999,或 0.0 到 9999.9,但您不想单独接受 0 或一系列零。
If that's the case, I'd say use something similar to ken's answerand process it as a float, then check that the float is above 0 and below 9999.9:
如果是这种情况,我会说使用类似于ken 的答案并将其作为浮点数处理,然后检查浮点数是否高于 0 并低于 9999.9:
var val = parseFloat($("#txt1").val());
if(isNan(val) || val <= 0 || val > 9999.9) {
alert("Please enter a value between 0 and 9999.9");
}
回答by Tim Pietzcker
If you need to use your regex and just add a check that the string doesn't consist entirely of zeroes, you can add a negative lookahead expression: (?!0+$)and enclose the alternation in a non-capturing group:
如果您需要使用您的正则表达式并只添加一个检查字符串不完全由零组成,您可以添加一个负前瞻表达式:(?!0+$)并将交替包含在非捕获组中:
/^(?!0+$)(?:[0-9][0-9]{0,3}|[0-9][0-9]{0,3}[\.][0-9])$/
回答by koketso mahlangu
No need for Regex or any of that, quite simple.
不需要正则表达式或任何一个,非常简单。
if(('#txt1').val() < 0.1 ){
alert("Please Enter Correct value");
}
There you go, problem solved
到了,问题解决了

