Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. function accountNumberValidator(accountNumber, countryCode) {
  2. let copy = accountNumber.replace(/[^0-9]+/g, '');
  3. const weights = [
  4. 1,
  5. 10,
  6. 3,
  7. 30,
  8. 9,
  9. 90,
  10. 27,
  11. 76,
  12. 81,
  13. 34,
  14. 49,
  15. 5,
  16. 50,
  17. 15,
  18. 53,
  19. 45,
  20. 62,
  21. 38,
  22. 89,
  23. 17,
  24. 73,
  25. 51,
  26. 25,
  27. 56,
  28. 75,
  29. 71,
  30. 31,
  31. 19,
  32. 93,
  33. 57,
  34. ];
  35. const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  36.  
  37. if (countryCode.split('').some(char => !alphabet.includes(char))) return false;
  38.  
  39. const countryCodeDigits = countryCode
  40. .toUpperCase()
  41. .split('')
  42. .map(char => alphabet.indexOf(char) + 10)
  43. .join('');
  44.  
  45. if (copy.length !== 26) return false;
  46.  
  47. copy += countryCodeDigits;
  48. copy = copy.substr(2) + copy.substr(0, 2);
  49. copy = copy.split('').reverse();
  50.  
  51. return copy.reduce((accumulator, current, i) => accumulator + current * weights[i], 0) % 97 === 1;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement