Class: OmgValidator::Validators::DecimalValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/omg_validator/validators/decimal_validator.rb

Overview

Checks whether input is a valid floating point number

validates :total_value, decimal: true

matches: 10.00, -23.12, 9.99, 1.9002302 does not match: 123, 99, -23, 1,000

Instance Method Summary (collapse)

Instance Method Details

- (Object) validate_each(record, attribute, value)



10
11
12
13
14
15
16
# File 'lib/omg_validator/validators/decimal_validator.rb', line 10

def validate_each(record, attribute, value)
  return nil if value.nil?
  reg = /^[\-+]?[0-9]+\.[0-9]+$/
  unless reg.match(value)
    record.errors[attribute] = "must be a valid floating point number"
  end
end