admin管理员组文章数量:1426314
I am using isNaN to evaluate input in text box my function is like this
function IsNumeric(n) {
return !isNaN(n);
}
it's working fine with numeric but not on negative values nor decimal values like 1.2,
but I will not accept negative or decimal values like 1.2
how can I do that?
Thanks
I am using isNaN to evaluate input in text box my function is like this
function IsNumeric(n) {
return !isNaN(n);
}
it's working fine with numeric but not on negative values nor decimal values like 1.2,
but I will not accept negative or decimal values like 1.2
how can I do that?
Thanks
Share Improve this question edited Feb 2, 2011 at 14:39 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Feb 2, 2011 at 14:27 airair 6,26426 gold badges95 silver badges126 bronze badges 1- n is any type text in text box.... – air Commented Feb 2, 2011 at 14:29
3 Answers
Reset to default 3You mean
function IsPostiveInteger(n) {
var n = new Number(n);
return !isNaN(n) && n===parseInt(n,10) && n>0;
}
Try to use Number(n)
. This will return NAN if its not a number. Else will return the same number irrespective of negative or positive
Shorter Alternate solution:
function IsNumeric(n){
// any valid number
//return /^-?\d+(?:\.\d+)?$/.test(n);
// only positive numbers
//return /^\d+(?:\.\d+)?$/.test(n);
// only positive whole numbers
return /^\d+$/.test(n);
}
本文标签: javascriptusing isNaN to evaluate inputStack Overflow
版权声明:本文标题:javascript - using isNaN to evaluate input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470040a2659706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论