Blog Image

validate credit card number using javascript

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;

}



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.