Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Check whether a given value satisfies the Luhn Algorithm
- is.validLuhn = function(value){
- var sum = 0, valuestr = value.toString().split("").reverse().join("");
- var luhnDigits = new Array();
- for(var i=0; i<valuestr.length; i++){
- if (i%2 != 0){
- console.log('Luhn Digit = '+valuestr.substr(i, 1));
- var luhnDigit = parseInt(valuestr.substr(i, 1))*2;
- if (luhnDigit>9){
- luhnDigitstr = luhnDigit.toString();
- luhnDigit = parseInt(luhnDigitstr.charAt(0)) + parseInt(luhnDigitstr.charAt(1));
- }
- luhnDigits.push(luhnDigit);
- }else{
- console.log('meh');
- luhnDigit = parseInt(valuestr.substr(i, 1));
- }
- if (i>0){
- sum += luhnDigit;
- }
- }
- console.log('Luhn Digits '+luhnDigits);
- console.log('Calculation sum '+sum);
- console.log('Verification Digit '+(sum*9)%10);
- console.log('Last Digit '+valuestr.substr(0, 1));
- var computedDigit = (sum*9)%10;
- return computedDigit.toString() == valuestr.substr(0, 1);
- console.log('Check digit = '+checkDigit);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement