|
Input, check & add check-digit to EAN 8/13 Overview: If you want to know how to validate the input of EAN 8/13 numbers ( http://en.wikipedia.org/wiki/European_Article_Number ) then this is for you! What do we need? 1 - A text input control is needed with the length of either 8 or 13 characters, depending on the code to be input. In case it is 13 in length, it will accept both EAN 8 and EAN 13 numbers. 2 - some code in the entry & exit sections of the control Entry -section: MySelf = NoSpace(MySelf) Exit-section MYSUM is intic is intTest is int IF MySelf <> Null THEN IF MySelf..Size <> 13 THEN Error("Program-Error: For input of EAN8/13-Numbers an Edit Control of length 13 is required !") ReturnToCapture(MySelf) END IF Length(NoSpace(MySelf))<>7 AND Length(NoSpace(MySelf))<>8 AND Length(NoSpace(MySelf))<>12 AND Length(NoSpace(MySelf))<>13 THEN Error("You have to input a valid number of characters:","for EAN-8 it is 7 or 8 numerics","for EAN-13 it is 12 or 13 numerics") ReturnToCapture(MySelf) END // Calculate check-digit // EAN is 7,8,12 or 13 digits in length // we use the first 7/12 digit to calculate the check-digit MYSUM = 0 IF Length(NoSpace(MySelf))<9 THEN // EAN 8 FOR ic = 1 TO 7 IF (ic/2) = IntegerPart(ic/2) THEN MYSUM = MYSUM + Val(Middle(MySelf, ic, 1)) * 1 ELSE MYSUM = MYSUM + Val(Middle(MySelf, ic, 1)) * 3 END END ELSE // EAN 13 FOR ic = 1 TO 12 IF (ic/2) = IntegerPart(ic/2) THEN MYSUM = MYSUM + Val(Middle(MySelf, ic, 1)) * 3 ELSE MYSUM = MYSUM + Val(Middle(MySelf, ic, 1)) * 1 END END END Test = MYSUM modulo 10 Test = 10 - Test IF Test = 10 THEN Test = 0 IF Length(NoSpace(MySelf)) = 7 OR Length(NoSpace(MySelf)) = 12 THEN MySelf = NoSpace(MySelf)+NoSpace(NumToString(Test)) ELSE IF Right(NoSpace(MySelf),1)<>NoSpace(NumToString(Test)) THEN // Oh - error on iput!! Error("Input Error: Input of an EAN8 /EAN13 - Number!", ... "Either you input the correct EAN with 8/13 digits or", ... "input 7/12 digits only. In the latter case the check-digit", ... "will be automatically added to the EAN !") ReturnToCapture(MySelf) END END END Note: As described above, you will be able to enter the full 8 / 13 digits of an EAN and it will return an error only just in case the input was bad. If you add 7 / 12 digits only, the check-digit will be added silently. |