验证非负整数和十进制值 [英] Validation for non-negative integers and decimal values

查看:41
本文介绍了验证非负整数和十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字段是:税率tax amount 我想在其中验证正值.

My fields are: tax rate and tax amount in which I want to validate positive values.

我写了这个验证:

:format => { :with => /\A[+]?\d+\Z/}

但它不会像4.67那样取带小数点的数字.它给我一个错误.什么类型的验证适用于整数和浮点值?例如:25754.56 应该通过但-2.56-87> 应该失败.

But it is not taking numbers with a decimal point like 4.67. And it's throwing me an error. What type of validation will work on integers and floating point values? for example: 2, 57, 54.56 should pass but -2.56, -87 should fail.

推荐答案

这不行吗?

validates :your_field, :numericality => { :greater_than_or_equal_to => 0 }

(按照规则猜测税收会更正确:)

(guess for taxes following rule will be more correct:)

validates :your_field, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100 }

这篇关于验证非负整数和十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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