使用 rails 3 验证数字 [英] Validate number with rails 3

查看:37
本文介绍了使用 rails 3 验证数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何验证文本字段中的输入是否为数字?not_integer 不是我想要的.可以是十进制数.

How can I validate if the input in a text field is a number? not_integer is not what I am looking for. It can be a decimal number.

推荐答案

您可以检查 numericality

validates :points, numericality: true

如果您想要更通用的方法,可以使用 is_a?.Ruby 中的父数字类是 Numeric.

If you want a more general approach, you can use is_a?. The parent number class in Ruby is Numeric.

a = 4
a.is_a? Numeric
=> true

b = 5.4
b.is_a? Numeric?
=> true

c = "apple"
c.is_a? Numeric
=> false

d = "4"
d.is_a? Numeric
=> false

这篇关于使用 rails 3 验证数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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