PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → Calculation of check-digits for EAN/UPC 8/13
Calculation of check-digits for EAN/UPC 8/13
Iniciado por Guenter Predl, 07,jun. 2004 13:54 - No hay respuesta
Publicado el 07,junio 2004 - 13:54
The edit control for input of an EAN8/EAN13/UPC8/UPC13/BBN/BBS number has have an input length of 8 or 13, the input mask should be 'Digits only'
Summe is int
i is int

Add the following code to the 'Exit of ...' of that control:
IF MySelf..Size <> 8 AND MySelf..Size <> 13 THEN
Error("For input of an EAN8/13 number you need an edit control of length 8 or 13!")
RETURN
END
// Check sum / check digit
IF Length(NoSpace(MySelf)) <> 0 THEN
// We act only on a filled control
IF (MySelf..Size = 8 AND Length(NoSpace(MySelf)) < 7) OR (MySelf..Size = 13 AND Length(NoSpace(MySelf)) < 12) THEN
Error("too few digits input!")
ReturnToCapture(MySelf)
END
// EAN/BBN/BBS number is of 7,8,12 or 13 digits length
// Let's calculate the check-digit out of the first 7/12 digits!
Summe = 0
FOR i = 1 TO (MySelf..Size - 1)
IF (i/2) = IntegerPart(i/2) THEN
IF MySelf..Size = 8 THEN
Summe = Summe + Val(Middle(MySelf, i, 1)) * 1
ELSE
Summe = Summe + Val(Middle(MySelf, i, 1)) * 3
END
ELSE
IF MySelf..Size = 8 THEN
Summe = Summe + Val(Middle(MySelf, i, 1)) * 3
ELSE
Summe = Summe + Val(Middle(MySelf, i, 1)) * 1
END
END
END
Test = Summe modulo 10
Test = 10 - Test
IF Test = 10 THEN Test = 0
IF Length(NoSpace(MySelf)) = (MySelf..Size - 1) THEN
MySelf = NoSpace(MySelf) + NoSpace(NumToString(Test))
ELSE IF Length(NoSpace(MySelf)) = MySelf..Size THEN
IF Right(NoSpace(MySelf),1)<>NoSpace(NumToString(Test)) THEN
// Aha - Error on wrong input of 8 or 13 digits with a bad check-digit!
Error("Failure on input of the EAN8/EAN13/BBN/BBS-number!", ...
"Either input this number correctly with 8/13 digits", ...
"or input the first 7/12 digits only, then the", ...
"check-digit will be added automatically!")
ReturnToCapture(MySelf)
END
END
END