I've got a situation where the users are entering characters into fields on the screen that I do not want them to enter. In COBOL, I can check these fields for alphabetic and numeric but I am under the impresion that this would not allow certain special characters that I do want to allow. Is there a way, short of checking each character one at a time, that would allow me to check these fields with special characters?
Software/Hardware used:
ASKED:
December 5, 2005 4:35 PM
UPDATED:
December 8, 2005 1:40 PM
I’m not sure, but suspect you may be dealing with fields greater than 1 character in length, so the COBOL soluntion would have to loop through all of the characters. Error detection should be done as close as possible to the source so a COBOL approach is probably best. If you find yourself in a situation where an Oracle solution is desirable, TRANSLATE might be useful. The following function will take strings up to 100 characters long and determine whether they are limited to the character set consisting of numbers, letters, underscores, hyphens and forward slashes. Those characters were chosen arbritrarily. Others could be added or substituted. If the 3rd parameter of the translate function is not at least as long as the 2nd parameter, some of the characters at the end of the 2nd parameter will not be translated.
CREATE OR REPLACE FUNCTION is_valid (
p_in_field varchar2)
RETURN varchar2
IS
v_is_valid varchar2(1);
v_1_char varchar2(1);
v_test_string varchar2(100);
v_translated_field varchar2(100);
BEGIN
v_is_valid := ‘F’;
v_1_char := ‘ ‘;
SELECT translate(p_in_field,’ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/’,
‘ ‘)
INTO v_translated_field
FROM dual;
SELECT rpad(v_1_char,length(p_in_field),’ ‘)
INTO v_test_string
FROM dual;
IF v_translated_field = v_test_string THEN
v_is_valid := ‘T’;
ELSE
v_is_valid := ‘F’;
END IF;
RETURN v_is_valid;
END is_valid;
Though I believe Blue Knight’s solution will work you could also check to see if the data entered is alphabetic
If Your-Field is Alphabetic
Perform Good-Routine
Else
Perform Error-Routine
EndIf.
You can use the following translate tables and the INSPECT verb to change the chars you don’t want to somehting else. Then compare the original field to the translated field and if there is a difference, you know you have unwanted characters in the input field.
In my example, the 1st(virgin) translate table is what you would use to make up your own ‘special’ table.
The second translate table is what I use to change any characters that would mess up a 3270 screen to spaces.
If you want me to send it to in a text file, let me know you email address & I will.
************************************************************************
* TRANSLATING/CONVERTING GARBAGE TO GOOD STUFF ********************
************************************************************************
01 FROM-CHARACTERS.
* 0 1 2 3 4 5 6 7 8 9 A B C D E F .
05 FC-00 PIC X(16) VALUE X’000102030405060708090A0B0C0D0E0F’.
05 FC-10 PIC X(16) VALUE X’101112131415161718191A1B1C1D1E1F’.
05 FC-20 PIC X(16) VALUE X’202122232425262728292A2B2C2D2E2F’.
05 FC-30 PIC X(16) VALUE X’303132333435363738393A3B3C3D3E3F’.
05 FC-40 PIC X(16) VALUE X’404142434445464748494A4B4C4D4E4F’.
05 FC-50 PIC X(16) VALUE X’505152535455565758595A5B5C5D5E5F’.
05 FC-60 PIC X(16) VALUE X’606162636465666768696A6B6C6D6E6F’.
05 FC-70 PIC X(16) VALUE X’707172737475767778797A7B7C7D7E7F’.
05 FC-80 PIC X(16) VALUE X’808182838485868788898A8B8C8D8E8F’.
05 FC-90 PIC X(16) VALUE X’909192939495969798999A9B9C9D9E9F’.
05 FC-A0 PIC X(16) VALUE X’A0A1A2A3A4A5A6A7A8A9AAABACADAEAF’.
05 FC-B0 PIC X(16) VALUE X’B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF’.
05 FC-C0 PIC X(16) VALUE X’C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF’.
05 FC-D0 PIC X(16) VALUE X’D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF’.
05 FC-E0 PIC X(16) VALUE X’E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF’.
05 FC-F0 PIC X(16) VALUE X’F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF’.
* 0 1 2 3 4 5 6 7 8 9 A B C D E F
01 FROM-CHARS REDEFINES FROM-CHARACTERS PIC X(256).
01 TO-CHARACTERS.
* 0 1 2 3 4 5 6 7 8 9 A B C D E F .
05 TC-00 PIC X(16) VALUE X’40404040404040404040404040404040′.
05 TC-10 PIC X(16) VALUE X’40404040404040404040404040404040′.
05 TC-20 PIC X(16) VALUE X’40404040404040404040404040404040′.
05 TC-30 PIC X(16) VALUE X’40404040404040404040404040404040′.
05 TC-40 PIC X(16) VALUE X’40404040404040404040404B4C4D4E40′.
05 TC-50 PIC X(16) VALUE X’504040404040404040405A5B5C5D5E40′.
05 TC-60 PIC X(16) VALUE X’60614040404040404040406B6C6D6E6F’.
05 TC-70 PIC X(16) VALUE X’40404040404040404040407B7C7D7E7F’.
05 TC-80 PIC X(16) VALUE X’40818283848586878889404040404040′.
05 TC-90 PIC X(16) VALUE X’40919293949596979899404040404040′.
05 TC-A0 PIC X(16) VALUE X’4040A2A3A4A5A6A7A8A9404040404040′.
05 TC-B0 PIC X(16) VALUE X’40404040404040404040404040404040′.
05 TC-C0 PIC X(16) VALUE X’C0C1C2C3C4C5C6C7C8C9404040404040′.
05 TC-D0 PIC X(16) VALUE X’D0D1D2D3D4D5D6D7D8D9404040404040′.
05 TC-E0 PIC X(16) VALUE X’4040E2E3E4E5E6E7E8E9404040404040′.
05 TC-F0 PIC X(16) VALUE X’F0F1F2F3F4F5F6F7F8F9404040404040′.
* 0 1 2 3 4 5 6 7 8 9 A B C D E F
01 TO-CHARS REDEFINES TO-CHARACTERS PIC X(256).
INSPECT HS-SIDEMARK-SHIP-MSG CONVERTING
FROM-CHARS TO TO-CHARS
I agree with TheQuigs solution. I’ve used the SPECIAL-NAMES section quite often. While many of the suggestions are good, this is probably the easiest to implement and the most flexible. A SPECIAL-NAMES class can be used against any field, unlike an 88-level which applies only to the field it is defined to. Here is a sample I used, one to check for alphanumeric characters that are found in SQL, another to include wild card chars (underscore/percent). In my case, I was validating database and tablespace names, albeit a bit simplistically.
SPECIAL-NAMES. CLASS VALID-SQLID IS ’0′ THRU ’9′
‘A’ THRU ‘I’
‘J’ THRU ‘R’
‘S’ THRU ‘Z’
‘ ‘
CLASS VALID-SQLID-WILD IS ’0′ THRU ’9′
‘A’ THRU ‘I’
‘J’ THRU ‘R’
‘S’ THRU ‘Z’
‘_’ ‘ ‘ ‘%’ ‘+’
.
Evaluate true
when ws-tsname is valid-sqlid
and ws-tsname (1:1) is alphabetic
and ws-tsname (1:1) not = space
.
.
.
when ws-tsname-wild-txt is valid-sqlid-wild
.
.
.
Hope this helps.
I really like TheQuigs solution. It keeps things simple like a level 88 but is far more useful since it can be used for any field. Thanks for posting that.