function isValidCreditCard(cardNo)
{
var str=cardNo;
var digitsOnly = str.replace(/ /g,”);//replace unwanted space
var sum = 0;
var digit = 0;
var addend = 0;
var timesTwo = false;
var i;
for(i = digitsOnly.length-1;i >= 0; i–)
{
digit = parseInt(digitsOnly.substring(i,i+1));
if (timesTwo)
{
addend = digit * 2;
if (addend > 9)
{
addend -= 9;
}
}
else
{
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}

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

}

validate credit card number using javascript

Leave your comment