Class Condition
Class Condition
CLASS condition tests whether the data in a variable belongs to a specific category of characters such as ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER, NUMERIC, etc. It also verifies the data that belongs to a user-defined category coded by CLASS in SPECIAL-NAMES.
Syntax -
Parameters -
- identifier-1 - Specifies the variable to validate.
- NOT - used to validate the negate condition.
- NUMERIC - Used to validate whether the data is numeric or not.
- ALPHABETIC - Used to validate the data contains only A to Z (lowercase or uppercase) characters and a space.
- ALPHABETIC-UPPER - Used to validate the data contains only uppercase A to Z characters and a space.
- ALPHABETIC-LOWER - Used to validate the data contains only lowercase a to z characters and a space.
- class-name - Used to validate the data contains characters listed in the CLASS definition of the SPECIAL-NAMES paragraph.
Valid class condition combinations for different data types -
Data Type in Declaration | Valid class condition |
---|---|
Alphabetic | ALPHABETIC ALPHABETIC-LOWER ALPHABETIC-UPPER class-name NOT ALPHABETIC NOT ALPHABETIC-LOWER NOT ALPHABETIC-UPPER NOT class-name |
Alphanumeric, alphanumeric-edited, numeric-edited |
ALPHABETIC ALPHABETIC-LOWER ALPHABETIC-UPPER NUMERIC class-name NOT ALPHABETIC NOT ALPHABETIC-LOWER NOT ALPHABETIC-UPPER NOT NUMERIC NOT class-name |
Decimal | NUMERIC NOT NUMERIC |
Numeric | NUMERIC class-name NOT NUMERIC NOT class-name |
Practical Example -
Scenario1 - Validating Alphabetic
05 WS-VAR1 PIC A(10) VALUE "MAINFRAME ".
...
IF WS-VAR1 IS ALPHABETIC
DISPLAY "Data is alphabetic"
ELSE
DISPLAY "Data is not alphabetic"
END-IF
Result - Data is alphabetic
Scenario2 - Validating Numeric value
05 WS-VAR2 PIC 9(02) VALUE 25.
...
IF WS-VAR2 IS NUMERIC
DISPLAY "Data is numeric"
ELSE
DISPLAY "Data is not numeric"
END-IF
Result - Data is numeric
Scenario3 - Validating custom class
SPECIAL-NAMES.
CLASS ALPHA-NUMERIC IS 'A' THROUGH 'Z' '0' THROUGH '9'.
...
05 WS-VAR1 PIC X(10) VALUE "MAINFRAME1".
...
IF WS-VAR1 IS ALPHA-NUMERIC
DISPLAY "Data is Alpha-numeric"
ELSE
DISPLAY "Data is not Alpha-numeric"
END-IF
Result - Data is Alpha-numeric