Sha256: 3ceb802dedc49bdb13c1abc0fe1ed805bd463d61a61ee0ced88e90a165f1ed57

Contents?: true

Size: 662 Bytes

Versions: 4

Compression:

Stored size: 662 Bytes

Contents

module OmgValidator
  module Validators
    # Checks whether input is a valid postal code or a valid zip code
    #
    # validates :zip_code, postal_or_zip_code: true
    class PostalOrZipCodeValidator < ActiveModel::EachValidator
      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
    end
  end
end

ActiveModel::Validations::PostalOrZipCodeValidator = OmgValidator::Validators::PostalOrZipCodeValidator

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
omg_validator-0.0.6.2 lib/omg_validator/validators/postal_or_zip_code_validator.rb
omg_validator-0.0.6.1 lib/omg_validator/validators/postal_or_zip_code_validator.rb
omg_validator-0.0.6 lib/omg_validator/validators/postal_or_zip_code_validator.rb
omg_validator-0.0.5 lib/omg_validator/validators/postal_or_zip_code_validator.rb