Last active
July 13, 2021 21:26
-
-
Save 5iDS/86be05043dd3e22146b1e3d4f43bba7c to your computer and use it in GitHub Desktop.
Validate Passport Number - taken from http://www.highprogrammer.com/alan/numbers/mrp.html
This file contains 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 _validate_passport( passport ) { | |
//Utils._strict( [ String ], arguments ); // Thirdparty Function | |
//trim | |
passport = passport.replace( /(^\s+|\s+$)/g, '' ); | |
var passport_characters = passport.split(''); | |
if( passport_characters.length == 9 ) { | |
// Go straight to encoding | |
} else if (passport_characters.length < 9 ) { | |
// add padding | |
var padding_length = 9 - (passport_characters.length); | |
for (var i = 0; i < padding_length; i++) { | |
passport_characters.push('<'); | |
}; | |
} else { | |
throw "Please ensure you've provided a valid passport number, which should be 9 characters or less."; | |
} | |
var checksum = 0; | |
var intRegex = /^\d+$/; // test for integer | |
var encoding_data = _generate_encoding_data(); | |
var encoding_data_length = encoding_data.length; | |
// Passport Number has 9 characters...consisting of A-Z | 0-9 | |
for ( var i = 0; i < 10; i++ ) { | |
if( passport_characters[i] === undefined ) {} else { | |
switch(i) { | |
case 0: | |
case 3: | |
case 6: | |
case 9: | |
// value * weight 7 | |
checksum = _calculate_checksum( passport_characters[i], checksum, 7 ); | |
break; | |
case 1: | |
case 4: | |
case 7: | |
// value * weight 3 | |
checksum = _calculate_checksum( passport_characters[i], checksum, 3 ); | |
break; | |
case 2: | |
case 5: | |
case 8: | |
// value * weight 1 | |
checksum = _calculate_checksum( passport_characters[i], checksum, 1 ); | |
break; | |
default: | |
throw "Invalid Passport Number Character[ " + i + " ] " + passport_characters[i]; | |
break; | |
} | |
} | |
} | |
return checksum % 10; // return remainder which is the check digit | |
/** | |
* @private | |
* | |
* Return Array of values to use for Passport Characters | |
* | |
* @return {Array.encodingDb} | |
* | |
**/ | |
function _generate_encoding_data() { | |
var encodingDb = []; | |
var alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); | |
encodingDb.push({ | |
'character' : '<', | |
'encoding' : '0' | |
}); | |
var encode_start_digit = 10; //till 35 | |
for ( var i = 0; i < alphabet.length; i++ ) { | |
encodingDb.push({ | |
'character' : alphabet[i], | |
'encoding' : encode_start_digit | |
}); | |
encode_start_digit++; | |
} | |
return encodingDb; | |
} | |
/** | |
* @private | |
* | |
* Return Checksum value for | |
* | |
* @note encodingDb must be initialised first | |
* | |
* @return {Integer.checksum} | |
* | |
**/ | |
function _calculate_checksum( ppChar, checksum, muliplier ) { | |
if( intRegex.test(ppChar) ) { | |
checksum = checksum + ( ppChar * muliplier ); | |
} else { | |
for (var k = 0; k < encoding_data_length; k++) { | |
if( encoding_data[k] && ( encoding_data[k].character == ppChar.toLowerCase() ) ) { | |
checksum = checksum + ( encoding_data[k].encoding * muliplier ); | |
} | |
} | |
} | |
return checksum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment