JavaScript 中的表单验证数字范围(最小值/最大值)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5809759/
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
Form validation numeric range (minimum/maximum) in JavaScript
提问by Robert
Have a script that defines text as being a number using a regular expression:
有一个使用正则表达式将文本定义为数字的脚本:
function validateTextNumericInRange(textInputId)
{
var numRegex = /^[\d]+$/;
var textInput = document.getElementById(textInputId);
var valid = true;
if (numRegex.test(textInput.value) == false) {
alert('Value must be a number between 3-48');
valid = false;
}
return valid;
}
Need to use this format and provide a min/max range (arbitrary for now, say between 3 and 48). How would I modify this regex to complete and have a correct argument?
需要使用这种格式并提供最小/最大范围(现在是任意的,比如 3 到 48)。我将如何修改此正则表达式以完成并拥有正确的参数?
回答by Vivin Paliath
I don't understand your question. Do you mean that you want a number that is between 3 and 48 digits, or that the value of the number must be between 3 and 48?
我不明白你的问题。您的意思是您想要一个介于 3 到 48 位之间的数字,还是该数字的值必须介于 3 到 48 之间?
For the latter you don't need a regex:
对于后者,您不需要正则表达式:
function validateTextNumericInRange (textInputId) {
var textInput = document.getElementById(textInputId);
var value = parseInt(textInput.value, 10);
return (!isNaN(value) && value >= 3 && value <= 48);
}
A more generic solution:
更通用的解决方案:
function validateTextNumericInRange(textInputId, min, max) {
var textInput = document.getElementById(textInputId);
var value = parseInt(textInput.value, 10);
return (!isNaN(value) && value >= min && value <= max);
}
To test to see if a number is between 3 and 48 digits long, you can use the regular expression /^[0-9]{3, 48}$/
.
要测试一个数字的长度是否在 3 到 48 位之间,您可以使用正则表达式/^[0-9]{3, 48}$/
。
回答by stema
A regular expression would be hard and inflexible, and for your example it would be:
正则表达式会很难且不灵活,对于您的示例,它将是:
/^(?:[3-9]|[1-3][0-9]|4[0-8])$/
Better go with Vivins' solution.
最好采用 Vivins 的解决方案。
回答by Marcos Eusebi
I'm not a good JavaScript developer, but I know that in Java you can use this syntax to test for minimum and maximum length:
我不是一个好的 JavaScript 开发人员,但我知道在 Java 中你可以使用这个语法来测试最小和最大长度:
[1-9][0-9]{2,4}
[1-9][0-9]{min,max}
回答by John Hartsock
function isInteger(value) {
if ((value.toString()).replace(/^-\d|\d/, "").length == 0) {
return true;
}
return false;
}
function integerInRange(value, min, max) {
if (isInteger(value)) {
if (parseInt(value, 10) >= min && parseInt(value, 10 <= max)) {
return true;
} else {
return false; // Not in range
}
} else {
return false; // Not an integer
}
}
integerInRange( 55, 3, 48); // Returns false
integerInRange("55", 3, 48); // Returns false
integerInRange( 25, 3, 48); // Returns true
integerInRange("25", 3, 48); // Returns true
In your case you would need to call it this way:
在您的情况下,您需要这样称呼它:
integerInRange(document.getElementById(textInputId).value, 3, 48);