验证信用卡详细信息 [英] Validate credit card details
本文介绍了验证信用卡详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何验证信用卡。我需要做luhn检查。黑莓手机上有api吗?
How do I validate a credit card. I need to do luhn check. Is there an api in blackberry to do it?
推荐答案
您可以使用以下方法验证信用卡号
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;
}
这篇关于验证信用卡详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文