Sha256: 4474d2f43af6d15e0ffb1cd7f6345c56c134940c5e0079543ca601cd07ea641f

Contents?: true

Size: 862 Bytes

Versions: 2

Compression:

Stored size: 862 Bytes

Contents

class CreditCardValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    if options[:american_express]
      format = /^3[47][0-9]{13}$/
    elsif options[:diners_club]
      format = /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/
    elsif options[:discover]
      format = /^6(?:011|5[0-9]{2})[0-9]{12}$/
    elsif options[:jbc]
      format = /^(?:2131|1800|35\d{3})\d{11}$/
    elsif options[:master_card]
      format = /^5[1-5][0-9]{14}$/
    elsif options[:visa]
      format = /^4[0-9]{12}(?:[0-9]{3})?$/
    else
      format = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$/
    end

    unless value.to_s =~ format
      record.errors[attribute] << (options[:message] || I18n.t('errors.messages.credit_card'))
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flash_validators-1.1.0 lib/flash_validators/validators/credit_card_validator.rb
flash_validators-1.0.0 lib/flash_validators/validators/credit_card_validator.rb