Class: OmgValidator::Validators::PostalOrZipCodeValidator

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

Overview

Checks whether input is a valid postal code or a valid zip code

validates :zip_code, postal_or_zip_code: true

Instance Method Summary (collapse)

Instance Method Details

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



7
8
9
10
11
12
13
# File 'lib/omg_validator/validators/postal_or_zip_code_validator.rb', line 7

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