Kbase P113812: How to validate a field to ensure that only alphanumeric characters are entered
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  28/02/2006 |
|
Status: Unverified
GOAL:
How to validate a field to ensure that only alphanumeric characters are entered
GOAL:
How to validate a field to ensure that only characters in the ranges of 0-9, a-z and A-Z are entered
GOAL:
Sample field validation procedure to check that a character is in the ranges 0-9, a-z and A-Z
FIX:
Sample code to be placed in either a separate Procedure that can be run from the VALUE-CHANGED trigger or the VALUE-CHANGED trigger for a specific field:
DEFINE VARIABLE cVal AS CHARACTER NO-UNDO.
DEFINE VARIABLE p1 AS CHARACTER NO-UNDO.
DEFINE VARIABLE p2 AS CHARACTER NO-UNDO.
DEFINE VARIABLE errStatus AS LOGICAL EXTENT 3 INIT FALSE NO-UNDO.
DEFINE VARIABLE errValid AS LOGICAL INIT TRUE NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iPos AS INTEGER NO-UNDO.
DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
ASSIGN iPos = SELF:CURSOR-OFFSET
cVal = SUBSTR(SELF:SCREEN-VALUE, iPos - 1, 1)
iVal = ASC(cVal)
.
/* Is character numeric? */
errStatus[1] = (iVal > 47 AND iVal < 58).
/* Is character lower-case letter? */
errStatus[2] = (iVal > 64 AND iVal < 91).
/* Is character upper-case letter? */
errStatus[3] = (iVal > 96 AND iVal < 123).
DO i = 1 TO 3:
IF errStatus[i] THEN DO:
/* Does character meet any of the conditions? */
errValid = FALSE.
LEAVE.
END.
END.
/* If character fails to meet any condition then it is illegal and the error condition is still true */
IF errValid THEN DO:
cVal = SELF:SCREEN-VALUE.
IF LENGTH(cVal) >= iPos THEN DO:
/* Handles illegal characters in middle of string */
p1 = SUBSTR(cVal, 1, iPos - 2).
p2 = SUBSTR(cVal, iPos).
SELF:SCREEN-VALUE = p1 + p2.
END.
ELSE
/* Handles illegal characters at end of string */
SELF:SCREEN-VALUE = SUBSTR(cVal, 1, iPos - 2).
/* Reposition cursor to end of fill-in */
SELF:CURSOR-OFFSET = LENGTH(cVal) + 1.
END.
ELSE
LEAVE.