1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| function (value){ if (!$.isString(value) || value.length !== 18) { return false; } value = value.toUpperCase();
var valid = false; var array = []; var dist = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18, 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27, 'W': 28, 'X': 29, 'Y': 30, '10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', '16': 'G', '17': 'H', '18': 'J', '19': 'K', '20': 'L', '21': 'M', '22': 'N', '23': 'P', '24': 'Q', '25': 'R', '26': 'T', '27': 'U', '28': 'W', '29': 'X', '30': 'Y' }; var power = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
for (var i = 0, len = value.length - 1; i < len; i++) { var num = dist[value[i]]; if (num === undefined) { return false; } array.push(num); } var sum = 0; for (var i = 0, len = array.length; i < len; i++) { sum += array[i] * power[i]; } if (dist[31 - sum % 31] + '' === value.charAt(value.length - 1)) { valid = true; } return valid; }
|