Consultor Eletrônico



Kbase 14155: How to detect non integer characters in field or variable
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/15/2008
Status: Verified

GOAL:

How to make sure a character field contains only integers before attempting an integer conversion on the field.

FACT(s) (Environment):

Progress 4GL

FIX:

The program below shows one way to check for non-integer characters in a field or variable using the ASC() function.  Since the ASC value for integers is between 48 and 57, it is necessary to check a field one character at a time for an ASC value that falls outside this range:

DEFINE VARIABLE x AS CHARACTER NO-UNDO.
   DEFINE VARIABLE cnt AS INTEGER NO-UNDO.
   DEFINE VARIABLE s AS INTEGER NO-UNDO.

  UPDATE x WITH FRAME f.
   s = LENGTH(x).

  DO cnt = 1 TO s:
    IF ASC(SUBSTRING(x,1,1)) < 48 OR ASC(SUBSTRING(x,1,1)) > 57 THEN DO:
        MESSAGE "Non-numeric character detected !!!!".
           LEAVE.
       END.
   x = SUBSTRING(x,2,LENGTH(x)).
  END.