Skip to content

Instantly share code, notes, and snippets.

@maagmirror
Created November 17, 2020 22:44
Show Gist options
  • Save maagmirror/af8b7daac701f9d8317e46fccd42ccc8 to your computer and use it in GitHub Desktop.
Save maagmirror/af8b7daac701f9d8317e46fccd42ccc8 to your computer and use it in GitHub Desktop.
function validation_digit(ci){
var a = 0;
var i = 0;
if(ci.length <= 6){
for(i = ci.length; i < 7; i++){
ci = '0' + ci;
}
}
for(i = 0; i < 7; i++){
a += (parseInt("2987634"[i]) * parseInt(ci[i])) % 10;
}
if(a%10 === 0){
return 0;
}else{
return 10 - a % 10;
}
}
function validate_ci(ci){
ci = clean_ci(ci);
var dig = ci[ci.length - 1];
ci = ci.replace(/[0-9]$/, '');
return (dig == validation_digit(ci));
}
function random_ci(){
var ci = Math.floor(Math.random() * 10000000).toString();
ci = ci.substring(0,7) + validation_digit(ci);
return ci;
}
function clean_ci(ci){
return ci.replace(/\D/g, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment