credit_card_validator gem

I recently published a gem that provides credit card validation. It is basically a ruby port of the javascript credit card validator by Thomas Fuchs (madrobby).


  CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
# allow test numbers to be valid (for development) 
CreditCardValidator::Validator.options[:test_numbers_are_valid] = true
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
# limit the card types you allow
CreditCardValidator::Validator.options[:allowed_card_types] = [:visa, :mastercard]
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')

Supported card types:

  :amex, :discover, :diners_club, :master_card, :visa

Whitespace is stripped from the number automatically.

The following things are tested:

1. does the luhn validation code add up? (see

2. does the number range and length seem right? (see

3. is it one of several well-known test numbers?

Note: this only validates that the number is of a valid format, it does not check if it is an actual credit card number. You will need to talk to your payment gateway to learn that.

You can also use the validator to learn about the type of the card:

  # gives the type back as a string (visa, master_card, etc)
# etc. - works for all of the supported card types

To Install:

gem install tobias-credit_card_validator --source

The source is available on github