Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function accountNumberValidator(accountNumber, countryCode) {
- let copy = accountNumber.replace(/[^0-9]+/g, '');
- const weights = [
- 1,
- 10,
- 3,
- 30,
- 9,
- 90,
- 27,
- 76,
- 81,
- 34,
- 49,
- 5,
- 50,
- 15,
- 53,
- 45,
- 62,
- 38,
- 89,
- 17,
- 73,
- 51,
- 25,
- 56,
- 75,
- 71,
- 31,
- 19,
- 93,
- 57,
- ];
- const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- if (countryCode.split('').some(char => !alphabet.includes(char))) return false;
- const countryCodeDigits = countryCode
- .toUpperCase()
- .split('')
- .map(char => alphabet.indexOf(char) + 10)
- .join('');
- if (copy.length !== 26) return false;
- copy += countryCodeDigits;
- copy = copy.substr(2) + copy.substr(0, 2);
- copy = copy.split('').reverse();
- return copy.reduce((accumulator, current, i) => accumulator + current * weights[i], 0) % 97 === 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement