javascript 测试字段值是否为数字且不为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13838992/
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
Test if field value is numeric and not empty
提问by Vangel Tzo
Possible Duplicate:
Validate that a string is a positive integer
可能的重复:
验证字符串是否为正整数
So I want to check a form field if it's empty and to allow it to has only positive numeric numbers, no spaces or letters. So far I succeed the first part to check if the field is empty with the following code:
所以我想检查一个表单域是否为空,并允许它只有正数字,没有空格或字母。到目前为止,我使用以下代码成功检查了该字段是否为空的第一部分:
function validate(form) {
var elements = form.elements;
if (form.qty.value == "") {
alert("Please enter the field");
document.forms[0].qty.focus()
document.forms[0].qty.select()
return false;
}
return true;
}
Somewhere in the form:
某处的形式:
<form action="addtocart.php" method ="post" onsubmit="return validate(this);" />
...
<form action="addtocart.php" method ="post" onsubmit="return validate(this);" />
...
But now I want to allow only numbers as well as to check if the field is empty. I have found some scripts but I don't know how to merge the code into one valid script.
但现在我只想允许数字以及检查该字段是否为空。我找到了一些脚本,但我不知道如何将代码合并为一个有效的脚本。
回答by Levi Botelho
if (Number(form.qty.value) > 0) {
// Only executes if value is a positive number.
}
Note that if the value is an empty string the statement will not return true. The same goes for null
, NaN
(obviously) and undefined
. String representations of numbers are transformed into their numeric counterparts.
请注意,如果该值为空字符串,则该语句将不会返回 true。这同样适用于null
,NaN
(显然)和undefined
。数字的字符串表示被转换为它们的数字对应物。
回答by shift66
you can use regexp
your pattern is ^\d+$
.
你可以使用正则表达式
你的模式是^\d+$
。
^
- starting of the line$
- end of the line\d
- digit+
- 1 or more times
^
- 行首$
- 行尾\d
- 数字+
- 1 次或多次
if(mystr.match(/^\d+$/))
{
//
}
if you need the first symbol not to be 0
you should write /^(0|[^1-9]\d*)/
read regexp documentation to understand this pattern.
如果您不需要第一个符号,0
您应该编写/^(0|[^1-9]\d*)/
阅读正则表达式文档来理解这种模式。
回答by CoursesWeb
Try with search()
function and RegExp. Like this:
尝试使用search()
函数和 RegExp。像这样:
if (form.qty.value.search(/^([0-9\.]+)$/)==-1)
{
alert ("Please enter the field");
document.forms[0].qty.focus()
document.forms[0].qty.select()
return false;
}
回答by Aadit M Shah
Check the following answer: https://stackoverflow.com/a/10270826/783743
检查以下答案:https: //stackoverflow.com/a/10270826/783743
In your case I would do the following:
在您的情况下,我会执行以下操作:
function validate(form) {
return isPositive(+form.qty.value);
}
function isPositive(n) {
return +n === n && n >= 0;
}
Explanation:
解释:
- The expression
+form.qty.value
coerces the string to a number. If the string is empty or is not a number it will expressNaN
. - The function
isPositive
only returns positive ifn
is a number (+n === n
) and it's greater than or equal to zero (n >= 0
).
- 该表达式
+form.qty.value
将字符串强制转换为数字。如果字符串为空或不是数字,它将表示NaN
. isPositive
如果n
是数字 (+n === n
) 并且大于或等于零( ),则该函数仅返回正数n >= 0
。