0.0
No release in over 3 years
Low commit activity in last 3 years
Validate numbers using Luhn Algorithm
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17
~> 10.0
~> 3.0

Runtime

>= 3, <= 6
 Project Readme

LuhnAlgorithm

Inspired after reading about Luhn Algorithm (or Modulus 10 Algorithm) article (https://en.wikipedia.org/wiki/Luhn_algorithm) from Wiki, I made this gem to easily validate a variety of identification numbers, for example Social Insurance numbers, or Credit card numbers.

Installation

Add this line to your application's Gemfile:

gem 'luhn_algorithm'

And then execute:

$ bundle

Or install it yourself as:

$ gem install luhn_algorithm

Usage

class Member
  include ActiveModel::Model

  attr_accessor :social_insurance_number
  validates_luhn_algorithm_of :social_insurance_number
end

OR

class Member
  include ActiveModel::Model

  attr_accessor :social_insurance_number
  validates :social_insurance_number, luhn_algorithm: true
end
person = Member.new(social_insurance_number: 'a valid number')

person.valid? # => true

You can also pass in a customized message when validating an attribute with Luhn Algorithm, for example

validates_luhn_algorithm_of :social_insurance_number, message: 'is not in the correct format'

If you want to validate a number with Luhn algorithm on console, you can just type LuhnAlgorithm.valid?(<your_number_here>), for example:

require 'luhn_algorithm'

LuhnAlgorithm.valid?(4111111111111111)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/thukim/luhn_algorithm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the LuhnAlgorithm project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.