Sha256: 8b4dfa158dcc2e7e9512c77e2ed6392cc848e8abd6ffa5569ec3c71d39611f04

Contents?: true

Size: 810 Bytes

Versions: 2

Compression:

Stored size: 810 Bytes

Contents

# Custom Validations for Social Security Number format
#
# e.g. validates :ssn, :ssn_format => true
# e.g. validates :ssn, :ssn_format => { :allow_blank => true, :if => :method_returns_true }
class SsnFormatValidator < ActiveModel::EachValidator

  # Checks input against a social security regex that matches ###-##-#### with or without dashes
  #
  # @param [ActiveRecord::Base] record The record to validate
  # @param [Symbol] attribute The field on the record to validate
  # @param [String] value The value of the attribute
  def validate_each record, attribute, value
    format = /^\A([\d]{3}\-[\d]{2}\-[\d]{4}|[\d]{9})\Z$/
    message = attribute.to_s.humanize + ' does not match the format ###-##-####.'
    record.errors[attribute] << (options[:message] || message ) unless value =~ format
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
format_validators-0.0.8 app/validators/ssn_format_validator.rb
format_validators-0.0.7 app/validators/ssn_format_validator.rb