Example usage for com.liferay.portal.kernel.util Validator isLUHN

List of usage examples for com.liferay.portal.kernel.util Validator isLUHN

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util Validator isLUHN.

Prototype

public static boolean isLUHN(String number) 

Source Link

Document

Returns true if the string contains a valid number according to the Luhn algorithm, commonly used to validate credit card numbers.

Usage

From source file:com.liferay.util.CreditCard.java

License:Open Source License

public static boolean isValidNumber(String number, String type) {
    number = StringUtil.extractDigits(number);

    if (type.equals("visa")) {
        if (!number.startsWith("4")) {
            return false;
        }//from www . jav a2  s .  c om

        if ((number.length() != 13) && (number.length() != 16)) {
            return false;
        }
    } else if (type.equals("mastercard")) {
        if (!number.startsWith("51") && !number.startsWith("52") && !number.startsWith("53")
                && !number.startsWith("54") && !number.startsWith("55")) {

            return false;
        }

        if (number.length() != 16) {
            return false;
        }
    } else if (type.equals("discover")) {
        if (!number.startsWith("6011")) {

            return false;
        }

        if (number.length() != 16) {
            return false;
        }
    } else if (type.equals("amex")) {
        if (!number.startsWith("34") && !number.startsWith("35") && !number.startsWith("36")
                && !number.startsWith("37")) {

            return false;
        }

        if (number.length() != 15) {
            return false;
        }
    }

    return Validator.isLUHN(number);
}