如何测试变量是否为数字? [英] How to test if a variable is numeric?

查看:72
本文介绍了如何测试变量是否为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



伙计,


我有一个盒子需要数字输入 - 我想测试它(是的,

我也会进行服务端检查,但也需要客户检查。)

如何测试我的输入是数字的 - 数字可能意味着

正数或负数浮动数


例如:对于1,1.5,-1,-4.25返回true并返回任何

其他的false非数字输入...


谢谢

Randell D.

解决方案

< blockquote> Randell D.写道:


伙计,

我有一个盒子,需要数字输入 - 我想测试它(是的,
我也会进行服务端检查,但也需要客户端检查。

如何测试我的输入是数字的 - 数字可能意味着
正或负浮动数字

例如:对于1,1.5,-1,-4.25返回true,并为任何
返回false其他非数字输入...

感谢
Randell D。




我想你会找到你想要的任何东西这里:


< URL:http://www.merlyn.demon.co.uk/js-valid.htm#Val>

-

Rob


>例如:对于任何

其他非数字输入,返回true为1,1.5,-1,-4.25并返回false ...




typeof value ==''number''


如果你不担心NaN或Infinity就很好。如果你是,那么


typeof value ==''number''&& isFinite(value)


更好。

http://www.crockford.com/javascript/remedial.html


Douglas Crockford写道:

例如:对于1,1.5,-1,-4.25返回true,并为
任何其他非数字输入返回false ...



typeof value ==''number''

如果你不担心NaN或Infinity就很好。如果你是,那么

typeof value ==''number''&& isFinite(value)

更好。

http://www.crockford.com/javascript/remedial.html




谢谢 - 我找到了typeof的参考但是没有'我很确定如何使用它...


干杯

randell d。



Folks,

I have a box that expects numeric input - I''d like to test for it (yeah,
I''ll have service side checks too but also want client checks).

How can I test that my input is numeric - and numeric could mean
positive or negative floating numbers

For example: return true for 1, 1.5, -1, -4.25 and return false for any
other non-numeric input...

Thanks
Randell D.

解决方案

Randell D. wrote:


Folks,

I have a box that expects numeric input - I''d like to test for it (yeah,
I''ll have service side checks too but also want client checks).

How can I test that my input is numeric - and numeric could mean
positive or negative floating numbers

For example: return true for 1, 1.5, -1, -4.25 and return false for any
other non-numeric input...

Thanks
Randell D.



I think you''ll find whatever you want here:

<URL:http://www.merlyn.demon.co.uk/js-valid.htm#Val>
--
Rob


> For example: return true for 1, 1.5, -1, -4.25 and return false for any

other non-numeric input...



typeof value == ''number''

is good if you are not worried about NaN or Infinity. If you are, then

typeof value == ''number'' && isFinite(value)

is better.

http://www.crockford.com/javascript/remedial.html


Douglas Crockford wrote:

For example: return true for 1, 1.5, -1, -4.25 and return false for
any other non-numeric input...


typeof value == ''number''

is good if you are not worried about NaN or Infinity. If you are, then

typeof value == ''number'' && isFinite(value)

is better.

http://www.crockford.com/javascript/remedial.html



Thanks - I found reference of typeof but wasn''t quite sure how to use it...

Cheers
randell d.


这篇关于如何测试变量是否为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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