在javascript中检查textfield的值是否为整数 [英] checking if value of a textfield is integer in javascript

查看:57
本文介绍了在javascript中检查textfield的值是否为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查在文本框中输入的文本是否为整数?我使用NAN函数,但它也接受十进制值。

How can i check is a text entered in a textbox is an integer or not? I used the NAN function but it accepts decimal values too.

我该怎么做?是否有任何内置方法?

How can I do this? Is there any built-in method?

推荐答案

假设文本字段由变量 intfield引用,然后你可以这样检查:

Let's say the text field is referenced by the variable intfield, then you can check it like this:

var value = Number(intfield.value);
if (Math.floor(value) == value) {
  // value is an integer, do something based on that
} else {
  // value is not an integer, show some validation error
}

这篇关于在javascript中检查textfield的值是否为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆