Purpose
Check if a string is a valid GLN

A Global Location Number is a unique 13-digit numerical code designed to identify specific locations within a global supply chain. The last digit of a GLN is a check digit, calculated using a specific algorithm applied to the other 12 digits of the GLN.

Solution We use a "process" function in order to encapsulate the algorithm for reuse in different scripts.

GuiXT
// process_check_gln.txt
Parameter gln

// 13 digits?
Set V[x] "&V[gln]" regex="^\d{13}$"
if not Q[ok]
  Return ""
endif

// calculate GLN checksum
Set V[k] 0
Set V[m] 0
Set V[n] 0
label next_digit
if V[k<11]
  Set V[k] &V[k] + 1
  Set V[m] &V[m] + &V[gln](&V[k]-&V[k])
  Set V[k] &V[k] + 1
  Set V[n] &V[n] + &V[gln](&V[k]-&V[k])
  goto next_digit
endif

Set V[n] &V[n] * 3
Set V[s] &V[m] + &V[n]

// checksum ends with last gln digit ?
Set V[x] &V[s] regex="&V[gln](13-13)$"

// matching ?
if Q[ok]
  Return "X"
else
  Return ""
endif

Usage:
GuiXT
Process is_gln = check_gln "&V[mygln]"

if V[is_gln]
  // ok
else
  // wrong GLN format
endif

Components: InputAssistant