i am currently making sahi tests for registering member applications for my employer and i needed a way to automatically generate a GLN number with a correct control/check digit, so here's my solution:
///////// SAHI CODE //////////
// function call
var $gln_base_id_number = Math.floor(Math.random()*900000000000) + 100000000000; // 12-digit random number
var $gln_check_digit = generate_gln_check_digit($gln_base_id_number);
var $gln = $gln_base_id_number + "" + $gln_check_digit; // convert the number to a string
// function
function generate_gln_check_digit($gln_base_id_number) {
$gln_base_id_number = (""+$gln_base_id_number); // convert the number to a string
var $factor = 3;
var $sum = 0;
var $index = $gln_base_id_number.length;
for ($index; $index > 0; --$index) {
$sum = $sum + $gln_base_id_number.substring($index-1, $index) * $factor;
$factor = 4 - $factor;
}
return ((1000 - $sum) % 10);
}
//////////////////////////////////////////////
///////// JAVASCRIPT CODE //////////
// function call
var gln_base_id_number = Math.floor(Math.random()*900000000000) + 100000000000; // 12-digit random number
var gln_check_digit = generate_gln_check_digit(gln_base_id_number);
var gln = gln_base_id_number + "" + gln_check_digit; // convert the number to a string
// function
function generate_gln_check_digit(gln_base_id_number) {
gln_base_id_number = (""+gln_base_id_number); // convert the number to a string
var factor = 3;
var sum = 0;
var index = gln_base_id_number.length;
for (index; index > 0; --index) {
sum = sum + gln_base_id_number.substring(index-1, index) * factor;
factor = 4 - factor;
}
return ((1000 - sum) % 10);
}
//////////////////////////////////////////////
i made this based on:
* gs1's online tool, which includes this external form
* gs1's javascript
IT, computer and programming tutorials and tips that i couldnt find anywhere else using google, from my daily work as a Senior Developer of solutions using Java and Linux.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment