Friday, January 6, 2012

Luhn Algorithm Implemented in JS code

Hi,

Here's my Javascript code for the Luhn Algorithm.  Check it out.  Just let me know you've used it. It'll make me happy.  :D

    function luhnCheckSum(strAccountNumber)
                {         
               
                    //alert("Inside confirmAccountNumber function");                      
                    var accountArray = new Array();
                    var total = 0;
                    var i = 0;
                   
                    for(i = 0; i < strAccountNumber.length - 1; i++)
                    {
                       
                       
                   
                       
                        accountArray[i] = Number(strAccountNumber.charAt(i));
                       
                        //alert("I: " + i + " Digit: " + accountArray[i]);
                       
                        if ( (accountArray[i] > 9)  && (i%2 == 1) )
                        {
                            accountArray[i] = 1 + accountArray[i] % 10;
                           
                               
                        }
                           
                        total = total + accountArray[i];
                    }
                   
                    total *= 9;
                   
                    var lastDigit = 0;
                   
                    lastDigit = total % 10;
                   
                    var checkSumDigit = 0;
                   
                    checkSumDigit = Number(strAccountNumber.charAt(9));
                   
                    //alert("Checksum According to Calculation: " + lastDigit + " CheckSum Digit" + checkSumDigit);
                   
                    if (lastDigit === checkSumDigit)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                   
                }



No comments:

Post a Comment