我正在尝试验证用户在金额字段中输入的内容。
该字段是amount_money
此字段是在表单提交时验证的字符串
monetize :amount, :as => :amount_money
validates :amount, numericality: {only_integer: true}
validates :amount_money, numericality: {greater_than_or_equal_to: 0}
validate :amount_money_within_limit
validate :is_a_valid_number
我想确保没有字母或符号,并且金额在可接受的范围内。
这样做的代码是
def amount_money_within_limit
if amount_money && amount_money.cents > 10_000_00
errors.add(:amount_money, 'cannot exceed $10,000.')
end
if amount_money && amount_money.cents < 1_00
errors.add(:amount_money, 'Problem with Amount')
end
end
这适用于输入数字,数字和字母,字母,特殊字符,但是
如果我尝试 Bob - 验证开始,但如果我尝试 BBob - 验证被绕过。
如果输入包含 2 个彼此相邻的大写字母 - 它会失败。我尝试了一个小写字母 - 但这不适合该领域是货币化的(金钱宝石) - 如果有有效的输入,小写字母就会出错。
如果该字段的输入包含两个大写字母 - 所有验证都被绕过 所以像 AA 这样的东西不会被上述验证中的任何东西捕获