从文本框中获取整数值,如何检查它是NaN还是null等? [英] Getting a integer value from a textbox, how to check if it's NaN or null etc?
问题描述
我通过JavaScript从文本框中提取值。如果文本框为空,则返回 NaN
。我想返回一个空字符串,如果它是null,空等等。
I am pulling a value via JavaScript from a textbox. If the textbox is empty, it returns NaN
. I want to return an empty string if it's null, empty, etc.
我该怎么做? if(NAN = tb.value)
?
推荐答案
嗯,某事这里很可疑。
空文本框在什么浏览器中返回NaN?我从来没有见过这种情况,我无法重现它。
In what browser does an empty textbox return NaN? I've never seen that happen, and I cannot reproduce it.
文本框的值实际上是一个字符串。一个空文本框返回一个空字符串!
The value of a text box is, in fact a string. An empty text box returns an empty string!
哦,要检查是否有NaN,你应该使用:
Oh, and to check if something is NaN, you should use:
if (isNaN(tb.value))
{
...
}
注意: isNaN()
-function返回 true
表示无法解析为数字的任何内容,空字符串除外。这意味着它是一个很好的数字输入检查(比正则表达式容易得多):
Note: The isNaN()
-function returns true
for anything that cannot be parsed as a number, except for empty strings. That means it's a good check for numeric input (much easier than regexes):
if (tb.value != "" && !isNaN(tb.value))
{
// It's a number
numValue = parseFloat(tb.value);
}
这篇关于从文本框中获取整数值,如何检查它是NaN还是null等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!