Java credit card validation – Luhn Algorithm in java

In this post, we will see about Luhn Algorithm in java

Introduction

Luhn algorithm, also known as modulus 10 or mod 10 algorithm, is a simple checksum process for validating various identification numbers such as credit card numbers, Canadian social securities numbers.
This algorithm is designed to protect again mistyped or accidental error rather than malicious attacks. Most credit card companies adopted this algorithm as this was available in the public domain and can be used by anyone.

Here are the steps involved in Luhn Algorithms.


Step 1:
From the rightmost digit, we should double every second digit.

LuhnAlgorithmMulBy2


Step 2:
When we double the digits and get product in double digits, then we should add digits of the product.

LuhnAlgorithmSumDigits


Step 3:
Compute the sum of all the digits.


Step 4:
If total sum is divisible by 10 i.e. total sum modulo 10 is 0, then number is valid else it is not valid.

LuhnAlgorithmCheckValid

As 90 mod 10 is 0, hence this is valid credit card number.

Detailed implementation

Let’s create java program to implement the Luhn algorithm.

Output:

90
1358954993914435 is valid as per luhn algorithm

Here are the steps involved in above java program for luhn algorithm.

  1. Convert a String cardNumber to int array cardIntArray for processing
  2. Iterate cardIntArray from rightmost side starting from cardIntArray.length-2 with step of i=i-2
  3. Mulitply digit by 2
  4. If product is greater than 9, sum the product
  5. Assign result back to cardIntArray[i]
  6. Once the loop is over, compute sum of elements of cardIntArray
  7. if sum%10 is 0 then credit card number is valid else not valid as per luhn algorithm

Using Apache common validation

In case, you don’t want to implement luhn algorithm you can use Apache common validation library and validate credit card number using LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(creditCardNumber);

You need to add following dependency to pom.xml.

Let’s see with the help of example:

Output:

79927398713 is valid as per luhn algorithm

That’s all about Luhn Algorithm in java

Related Posts

  • 29 September

    Input validation in java using Scanner

    In this post, we will see how to do input validation in java using Scanner class. The Java Scanner class is used to get input from user. It provides several methods to get input of different types and to validate the input as well. Here we will see some of these methods that can be […]

  • 04 May

    Validate password in java

    In this post, we will see how to validate a password in java. Here are rules for password: Must have at least one numeric character Must have at least one lowercase character Must have at least one uppercase character Must have at least one special symbol among @#$% Password length should be between 8 and […]

  • 04 May

    Java regex for currency symbols

    In this post, we will see about regex to find currency symbols in a text. You can use below regex to find currency symbols in any text. \\p{Sc} Each unicharacter belongs to certain category and you can search for it using /p. Sc is short code for current symbol, so using \p{Sc}, we are trying […]

  • 21 March

    Java isNull method

    In this post, we will about Objects class’s isNull method. Objects’s isNull() method is used to check if object is null or not. java.util.Objects class was introduced in java 7. Here is simple example for Object's isNull method. [crayon-60090f507c774250332129/] Output: Is str1 null: false Is str2 null: true Let’s look at source code Object’s isNull() […]

  • 25 July

    Validate phone number in java

    In this post, we will see how to validate phone number in java. Validate any international phone number as per E.123 E.123 is a standards-based recommendation by the International Telecommunications Union sector ITU-T. E.123 provides following specifications. The leading plus (+) serves as an international prefix symbol, and is immediately followed by the country code […]

  • 11 July

    Validate email address in java

    In this post, we will see how to validate email address in java. There are times when you have to validate email address provided by user. You might want to write your own logic to validate email addresses but there are lots of standard regular expressions or libraries which can help you validate email address […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.