The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20130313

sahi/javascript - function to generate a random 13-digit gs1 gln number

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

No comments:

Post a Comment