Gem adds validator to check whether or not a given number actually falls within the ranges of possible numbers prior to performing such verification, and, as such, CreditCardValidations simply verifies that the credit card number provided is well-formed.
Add this line to your application's Gemfile:
gem 'credit_card_validations'
And then execute:
$ bundle
Or install it yourself as:
$ gem install credit_card_validations
The following issuing institutes are accepted:
name | key
-----------------------
American Express | :amex
China UnionPay | :unionpay
Dankrot | :dankrot
Diners Club | :diners
Dinner Club US | :diners_us
Discover | :discover
JCB | :jcb
Laser | :laser
Maestro | :maestro
MasterCard | :mastercard
Solo | :solo
Visa | :visa
Examples using string monkey patch
require 'credit_card_validations/string'
'5274 5763 9425 9961'.credit_card_brand
'5274 5763 9425 9961'.valid_credit_card_brand?(:mastercard, :visa)
'5274 5763 9425 9961'.valid_credit_card_brand?(:amex)
Examples using CreditCardValidations::Detector class
number = "4111111111111111"
detector = CreditCardValidations::Detector.new(number)
detector.brand #:visa
detector.visa? #true
detector.valid?(:mastercard,:maestro) #false
detector.valid?(:visa, :mastercard) #true
detector.issuer_category #"Banking and financial"
Also You can add your own rules to detect other credit card brands/types passing name,length(integer/array of integers) and prefix(string/array of strings) Example
CreditCardValidations::Detector.add_rule(:voyager, 15, '86')
voyager_test_card_number = '869926275400212'
CreditCardValidations::Detector.new(voyager_test_card_number).brand #:voyager
CreditCardValidations::Detector.new(voyager_test_card_number).voyager? #true
Check luhn
CreditCardValidations::Detector.new(@credit_card_number).valid_luhn?
ActiveModel support
only for certain brads
class CreditCardModel
attr_accessor :number
include ActiveModel::Validations
validates :number, credit_card_number: {brands: [:amex, :maestro]}
end
for all known brands
validates :number, presence: true, credit_card_number: true
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request