I am currently taking input in a text-box and on-submit, I am validating the input, the input needs to be a number greater than 0, it should be integer and not contain decimal point. The problem is I am not able to find any method by which we can detect it is integer or contains decimal point. Here is the code I am using.
我目前正在输入一个文本框和提交,我正在验证输入,输入需要是一个大于0的数字,它应该是整数,不包含小数点。问题是我无法找到任何可以检测到它是整数还是包含小数点的方法。这是我正在使用的代码。
txt[5] = $(this).parents('p').find('input:text').val();
if (txt[5] == "" || isNaN(txt[5]) || txt[5] <= 0)
{
alert("Incorrect Quantity");
return false;
}
Basically as you see I am validating the input given by user, I want to show them a message if the input is wrong, so I am currently using txt[5]%1!=0 to make sure only INT is allowed, but I am open to new answers.
基本上,当你看到我正在验证用户给出的输入时,如果输入错误,我想向他们显示一条消息,所以我目前正在使用txt [5]%1!= 0来确保只允许INT,但是我我愿意接受新的答案。
3 个解决方案
#1
3
txt[5] = parseInt($(this).parents('p').find('input:text').val(), 10);
This won't validate the number (see Nirk's answer for that), but if it's not an int, it will be converted to one.
这不会验证数字(请参阅Nirk的答案),但如果它不是int,则会转换为1。
#2
4
The simplest way is to parse the text as a Number, coerce to integer, and see if they are equal:
最简单的方法是将文本解析为数字,强制转换为整数,并查看它们是否相等:
var numbertxt5 = Number(txt[5]);
if(numbertxt5 | 0 == numbertxt5) { /* int */
} else if (!isNaN(numbertxt5)) { /* float */
} else { /* not a number */
#3
3
That is because JavaScript has only one numerical type called number
. It is neither int
nor float
, but it behaves like both at a time. However, you do have two methods that you can use for this parseInt
and parseFloat
.
这是因为JavaScript只有一种称为数字的数字类型。它既不是int也不是float,但它同时表现得像两者一样。但是,您有两个方法可用于此parseInt和parseFloat。
var result = parseInt($(this).parents('p').find('input:text').val());
txt[5] = result;
Using parseFloat
is similar. Eg.
使用parseFloat是类似的。例如。
var result = parseFloat($(this).parents('p').find('input:text').val());
txt[5] = result;
The second example is not what you want but it is here just for your reference.
第二个例子不是你想要的,但它只是供你参考。
UPDATE: @bfavaretto made a good point with using radix which I omitted in parseInt
. Have a look at this page for reference http://www.w3schools.com/jsref/jsref_parseint.asp
更新:@bfavaretto在使用我在parseInt中省略的基数时提出了一个很好的观点。请查看此页面以供参考http://www.w3schools.com/jsref/jsref_parseint.asp
#1
3
txt[5] = parseInt($(this).parents('p').find('input:text').val(), 10);
This won't validate the number (see Nirk's answer for that), but if it's not an int, it will be converted to one.
这不会验证数字(请参阅Nirk的答案),但如果它不是int,则会转换为1。
#2
4
The simplest way is to parse the text as a Number, coerce to integer, and see if they are equal:
最简单的方法是将文本解析为数字,强制转换为整数,并查看它们是否相等:
var numbertxt5 = Number(txt[5]);
if(numbertxt5 | 0 == numbertxt5) { /* int */
} else if (!isNaN(numbertxt5)) { /* float */
} else { /* not a number */
#3
3
That is because JavaScript has only one numerical type called number
. It is neither int
nor float
, but it behaves like both at a time. However, you do have two methods that you can use for this parseInt
and parseFloat
.
这是因为JavaScript只有一种称为数字的数字类型。它既不是int也不是float,但它同时表现得像两者一样。但是,您有两个方法可用于此parseInt和parseFloat。
var result = parseInt($(this).parents('p').find('input:text').val());
txt[5] = result;
Using parseFloat
is similar. Eg.
使用parseFloat是类似的。例如。
var result = parseFloat($(this).parents('p').find('input:text').val());
txt[5] = result;
The second example is not what you want but it is here just for your reference.
第二个例子不是你想要的,但它只是供你参考。
UPDATE: @bfavaretto made a good point with using radix which I omitted in parseInt
. Have a look at this page for reference http://www.w3schools.com/jsref/jsref_parseint.asp
更新:@bfavaretto在使用我在parseInt中省略的基数时提出了一个很好的观点。请查看此页面以供参考http://www.w3schools.com/jsref/jsref_parseint.asp