Created
August 5, 2015 09:02
-
-
Save FabienPapet/e3957415f0c33384fc72 to your computer and use it in GitHub Desktop.
RIO control for french phones in Javascript
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_isRIO(fullRIO, telephone) { | |
// fullRIO is id+ctlKey | |
var id = fullRIO.substr(0,fullRIO.length -3); | |
var ctlKey = fullRIO.substr(fullRIO.length -3, 3); | |
var ordre = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+"; | |
var a = b = c = 0; | |
var concat = id + telephone; | |
console.log(concat); | |
for (var i = 0; i < 19; i++) { | |
var position = ordre.indexOf(concat[i]); | |
a = (1 * a + position) % 37 ; | |
b = (2 * b + position) % 37 ; | |
c = (4 * c + position) % 37 ; | |
}; | |
var ccc = ""+ordre[a] + ordre[b] + ordre[c]; | |
if (ctlKey !== ccc) { | |
console.log(ccc,ctlKey); | |
return false; | |
} else { | |
var r = new RegExp("^[0-9]{2}[EP]{1}[A-Za-z0-9]{6}"+ctlKey+"$"); | |
console.log(r) | |
return (id +ctlKey).match(r) !== null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment