Skip to content

Instantly share code, notes, and snippets.

@Agowan
Created July 14, 2015 12:50
Show Gist options
  • Save Agowan/5da78ec363bf9a6f7ecc to your computer and use it in GitHub Desktop.
Save Agowan/5da78ec363bf9a6f7ecc to your computer and use it in GitHub Desktop.
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
}
@Agowan
Copy link
Author

Agowan commented Jul 14, 2015

Use this polyfill if not using ES6 for forEach funtionallity.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment