Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate credit card details

How do I validate a credit card. I need to do luhn check. Is there an api in blackberry to do it?

like image 993
user1121332 Avatar asked Jan 17 '12 13:01

user1121332


1 Answers

You can use the following method to validate a credit card number

// -------------------
// Perform Luhn check
// -------------------

public static boolean isCreditCardValid(String cardNumber) {
    String digitsOnly = getDigitsOnly(cardNumber);
    int sum = 0;
    int digit = 0;
    int addend = 0;
    boolean timesTwo = false;

    for (int i = digitsOnly.length() - 1; i >= 0; i--) {
        digit = Integer.parseInt(digitsOnly.substring(i, i + 1));
        if (timesTwo) {
            addend = digit * 2;
            if (addend > 9) {
                addend -= 9;
            }
        } else {
            addend = digit;
        }
        sum += addend;
        timesTwo = !timesTwo;
    }

    int modulus = sum % 10;
    return modulus == 0;

}
like image 151
rfsk2010 Avatar answered Oct 27 '22 04:10

rfsk2010