Class: OmgValidator::Validators::PostalOrZipCodeValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- OmgValidator::Validators::PostalOrZipCodeValidator
- 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
Matches:
L2J 4T5, l2a 9d1, h2o 4g1, h2o4g1, 90210, 20037-8001, 12345
Does not match:
z2n 1n3, aan 2j2, LL2J 4T5, 123456, 20037-001, 207-01
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/postal_or_zip_code_validator.rb', line 14 def validate_each(record, attribute, value) return nil if value.blank? 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 |