Home > Uncategorized > Luhn Check algorithm in Javascript

Luhn Check algorithm in Javascript

Luhn Check algorithm in Javascript

An integral part of the generation of IMEI numbers is the Luhn Check algorithm, which determines the final digit in an IMEI number

 

Source code:

 // Javascript code copyright 2009 
 // 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; for="" i="" (i="Luhn.length-1;" (0,1,2,3,4,-4,-3,-2,-1,0);="" array="" delta="new" var="" }="" +="parseInt(Luhn.substring(i,i+1));" sum="" {="" )="" 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;
 }
Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: