Last active
December 8, 2016 12:37
-
-
Save nickovchinnikov/0710f2a209e9078095f1db2aa090073b to your computer and use it in GitHub Desktop.
ISIN validator http://stackoverflow.com/a/16141878
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isinValidate(isin) { | |
isin = isin.toUpperCase(); | |
if (!/^[0-9A-Z]{12}$/.test(isin)) { | |
return false; | |
} | |
if (isin.length != 12) return false; | |
var v = []; | |
for (var i = isin.length - 2; i >= 0; i--) { | |
var c = parseInt(isin.charAt(i)); | |
if (isNaN(c)) { //not a digit | |
var letterCode = isin.charCodeAt(i) - 55; //Char ordinal + 9 | |
v.push(letterCode % 10); | |
if (letterCode > 9) { | |
v.push(Math.floor(letterCode / 10)); | |
} | |
} else { | |
v.push(Number(c)); | |
} | |
} | |
var sum = 0; | |
var l = v.length; | |
for (i = 0; i < l; i++) { | |
if (i % 2 == 0) { | |
var d = v[i] * 2; | |
sum += Math.floor(d / 10); | |
sum += d % 10; | |
} else { | |
sum += v[i]; | |
} | |
} | |
return (10 - (sum % 10)) % 10 === Number(isin[isin.length - 1]); | |
} | |
var isinArrayValid = [ | |
"US0378331005", | |
"AU0000XVGZA3", | |
"GB0002634946" | |
]; | |
isinArrayValid.map(function (item) { | |
console.log( | |
"ISIN test correct values: " + isinValidate(item) | |
); | |
}); | |
var isinArrayNonValid = [ | |
"US0373431005", | |
"AU0000GV4TA3", | |
"AU0000434ZA3", | |
"GB0002634Y46" | |
]; | |
isinArrayNonValid.map(function (item) { | |
console.log( | |
"ISIN test non correct values : " + isinValidate(item) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment