如何检查值是否为数字? [英] How can I check if a value is a number?
本文介绍了如何检查值是否为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想检查表单文本字段的返回值是否为数字,即: 12 、 12.5 或 12.75 .有没有一种简单的方法来检查这一点,尤其是当值被拉为 param
时?
I want to simply check if a returned value from a form text field is a number i.e.: 12 , 12.5 or 12.75. Is there a simple way to check this, especially if the value is pulled as a param
?
推荐答案
只需正则表达式即可,这很琐碎,除此之外不值得考虑:
Just regexp it, it's trivial, and not worth thinking about beyond that:
v =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
(根据贾斯汀的评论修正)
(Fixed as per Justin's comment)
这篇关于如何检查值是否为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文