credit card checksum is crashing my webpage

40 Views Asked by At
function validcc(ccnum)
{
var valid2 = new Boolean(true);
var nums = new Array();
var checknum = 0;

if (ccnum.length != 16)
{
    valid2=false;
}

for (var v=0; v<16; v+2)
{
    nums[v] = (ccnum.slice(v,v+1)*2)
    if (nums[v]>9)
    {
        nums[v] = (nums[v].slice(0,1) + nums[v].slice(1,2));
    }
}

for (var i=1; i<16; i+2)
{
    nums[i] = (ccnum.slice(i,i+1));
}

for (var j=0; j<16; j++)
{
    checknum = checknum + nums[j];
}

var rem = checknum%10;

if (rem !=0)
{
 valid2=false;
}

return valid2;
}

this is the function that checks if the credit card number given is valid! I have other functions also but they were all working fine until I added this one don't know what the problem is please help!

1

There are 1 best solutions below

0
On

All your loops: for (var v=0; v<16; v+2)

are not incrementing the control variable and so loop forever.

You would:

for (var v = 0; v < 16; v += 2)

Note that not all card numbers are 16 digits.

There are many luhn checks written in JavaScript, perhaps find a proven one.