Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A ruby gem for validating credit cards (a port of Thomas Fuchs' creditcard_js)

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 script
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 History.txt
Octocat-spinner-32 Manifest.txt
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 credit_card_validator.gemspec
README.rdoc

credit_card_validator

DESCRIPTION:

A gem that provides credit card validation. It is basically a ruby port of the javascript credit card validator by Thomas Fuchs (madrobby) (github.com/madrobby/creditcard_js).

SYNOPSIS:

Usage:

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, :maestro

Whitespace is stripped from the number automatically.

The following things are tested:

  1. does the luhn validation code add up? (see en.wikipedia.org/wiki/Luhn_algorithm)

  2. does the number range and length seem right? (see en.wikipedia.org/wiki/Bank_card_number)

  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)
CreditCardValidator::Validator.card_type(number)

CreditCardValidator::Validator.is_visa?(number)
CreditCardValidator::Validator.is_master_card?(number)
# etc. - works for all of the supported card types

CreditCardValidator::Validator.is_allowed_card_type?(number)

INSTALL:

  • gem install credit_card_validator

LICENSE:

Copyright © 2009-2011 Tobias Crawley & Bruce Hauman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.