Class: OmgValidator::Validators::ZipCodeValidator

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

Overview

Checks whether input is a valid zip code

Matches:

90210, 20037-8001, 12345

Does not match:

123456, 20037-001, 207-01

Examples:

validates :zip_code, zip_code: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/omg_validator/validators/zip_code_validator.rb', line 14

def validate_each(record, attribute, value)
  return nil if value.blank?
  reg = /^\d{5}(-\d{4})?$/i
  unless reg.match(value)
    record.errors[attribute] = "must be a valid zip code"
  end
end