Created
July 14, 2015 12:50
-
-
Save Agowan/5da78ec363bf9a6f7ecc to your computer and use it in GitHub Desktop.
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
var validatePnr = function(str) { | |
if(str.length != 12) | |
return false; | |
var date_string = [str.slice(0,4),'-',str.slice(4,6),'-',str.slice(6,8)].join(''); | |
if(isNaN(Date.parse(date_string))) | |
return false; | |
str = str.slice(2,12); | |
var sum = 0 | |
numbers = str.split('').map(function(n){ return parseInt(n)}); | |
numbers.forEach(function(n, index){ | |
if(index % 2 == 0) n *= 2; | |
if(n >= 10) n -= 9 | |
sum += n | |
}); | |
return sum % 10 == 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this polyfill if not using ES6 for forEach funtionallity.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach