/**
* Created by Xeis con k on 30/12/2014.
*/
public class LuhnAlgorithm {
public static boolean isValidCardNumber(String cardNumber){
int suma = 0;
int digito = 0;
int sumando = 0;
boolean doble = false;
for (int i = cardNumber.length()-1 ; i >= 0 ; i--){
try {
digito = Integer.parseInt(cardNumber.substring(i, i + 1));
}catch (NumberFormatException e){
//Log.e("LuhnAlgorithm","Se encontro un caracter no numerico: "+cardNumber); //<- log para android
return false;
}
if(doble){
sumando = digito * 2;
if(sumando > 9){
sumando -= 9;
}
}else{
sumando = digito;
}
suma += sumando;
doble = !doble;
}
return (suma % 10) == 0;
}
}
Algoritmo que se usa para validar una tarjeta de credito
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.