Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. //Check whether a given value satisfies the Luhn Algorithm
  2. is.validLuhn = function(value){
  3. var sum = 0, valuestr = value.toString().split("").reverse().join("");
  4. var luhnDigits = new Array();
  5. for(var i=0; i<valuestr.length; i++){
  6.  
  7. if (i%2 != 0){
  8.  
  9. console.log('Luhn Digit = '+valuestr.substr(i, 1));
  10. var luhnDigit = parseInt(valuestr.substr(i, 1))*2;
  11. if (luhnDigit>9){
  12. luhnDigitstr = luhnDigit.toString();
  13. luhnDigit = parseInt(luhnDigitstr.charAt(0)) + parseInt(luhnDigitstr.charAt(1));
  14. }
  15.  
  16. luhnDigits.push(luhnDigit);
  17.  
  18. }else{
  19. console.log('meh');
  20. luhnDigit = parseInt(valuestr.substr(i, 1));
  21. }
  22. if (i>0){
  23. sum += luhnDigit;
  24. }
  25. }
  26. console.log('Luhn Digits '+luhnDigits);
  27.  
  28. console.log('Calculation sum '+sum);
  29. console.log('Verification Digit '+(sum*9)%10);
  30. console.log('Last Digit '+valuestr.substr(0, 1));
  31. var computedDigit = (sum*9)%10;
  32.  
  33. return computedDigit.toString() == valuestr.substr(0, 1);
  34. console.log('Check digit = '+checkDigit);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement