DEV Community

vidhya murali
vidhya murali

Posted on

Credit card validation using Luhn algorithm or mod 10 algorithm

The Luhn algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, Canadian Social Insurance Numbers

function check(cardno){

let nDigit=cardno.length;
let sum=0;
let isSecond=false;

for(let i=nDigit-1;i>=0;i--){
let d=Number(cardno[i]);
    if(isSecond){
        d=d*2
    }

    sum+=parseInt(d/10);
    sum+=d%10;

    isSecond=!isSecond;
}

if(sum%10==0){
    return true;
}
else{
    return false
}

}

if(check(79927398713)){
    console.log("This is a Valid Card");
}
else{
    console.log("This is not a  Valid Card");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)