Need help with Luhn check

Discussion in 'JavaScript' started by niso, Oct 7, 2010.

  1. #1
    I need to output 350 employee numbers with a Luhn number at the end of the string.
    I found this site: http://imei.sms.eu.sk/ This is almost what I want, but with this code I only can implement one string at a time, so with 350 numbers it will take time. Do anyone know how I can insert all the 350 employee numbers at once and get the Luhn number at the end of the string?

    Source code:
    
     // Javascript code copyright 2009 by Fiach Reid : www.webtropy.com
     // This code may be used freely, as long as this copyright notice is intact.
     function Calculate(Luhn)
     {
        var sum = 0;
        for (i=0; i<Luhn.length; i++ )
        {
            sum += parseInt(Luhn.substring(i,i+1));
        }
        var delta = new Array (0,1,2,3,4,-4,-3,-2,-1,0);
        for (i=Luhn.length-1; i>=0; i-=2 )
        {        
            var deltaIndex = parseInt(Luhn.substring(i,i+1));
            var deltaValue = delta[deltaIndex];    
            sum += deltaValue;
        }    
        var mod10 = sum % 10;
        mod10 = 10 - mod10;    
        if (mod10==10)
        {        
            mod10=0;
        }
        return mod10;
     }
    
     function Validate(Luhn)
     {
        var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length));
        var LuhnLess = Luhn.substring(0,Luhn.length-1);
        if (Calculate(LuhnLess)==parseInt(LuhnDigit))
        {
            return true;
        }    
        return false;
     }
    Code (markup):
     
    niso, Oct 7, 2010 IP