How to prevent user from entering special characte

2019-08-05 10:23发布

问题:

How to prevent user from entering special characters (like symbols not visible in keyboard) in a text field (Character type) in AS400 ?? Visibly most of the time there is no issue for iseries/as400 in accepting it and saving it into the file but sometimes when the data is handled in downstream reporting system it get rejected with different error in their system. Is there a way to prevent from the entry point itself ?

eg: In Address field of a Customer, end user copy paste (ctrl + c ctrl + v ) details from a webpage or a document causing entry of junk characters or null values which get stored without issue but later cause problem in other reporting applications.

回答1:

Validate the screen field using the %CHECK function and display an error when invalid content is present.

D allow           C                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ+
D                                      abcdefghijklmnopqrstuvwxyz+
D                                      0123456789'

/free
    if %check(allow:scrfld01) > 0;
        error = 1;
    endif; 
/end-free

Alternately you could iterate through the screen field removing any characters which are not present in the allowed character set prior to saving the record.



回答2:

Not sure if you're asking how to find the characters within the program that accepts the data, or how to prevent all programs from writing such data. I'll assume it's the latter.

The only way I know to do this with certainty, is with a trigger program; the system doesn't have a blacklist of characters that are/not allowed in fields (and you wouldn't want that anyway).

Hopefully there are only a few tables (files) that concern you in this manner, and you could write a trigger program, to be attached BEFORE_UPDATE and BEFORE_INSERT for each table, that checks the fields in question and either:

Changes any control characters it sees (this is generally bad form)

  or

 > Fails the write, allowing the entry program to re-prompt the user (most 
   programmers do not properly handle errors - especially write errors - so 
   this solution is iffy)

If you have a lot of files like this, a generic trigger can be set up that will determine the layout of the table in question (the table name is a parameter to the trigger) using the Retrieve Fields API. Be wary of a performance hit.



回答3:

Most issues related to invalid characters are listed here. You could use it on any character field with the %XLATE opcode. Example:

In your "D" specs use this...

     * Special Characters used for EBCDIC converision using CCSID 37       ----                
     * Invalid Control Characters not allowed in XML, including 
     * Plus CR and LF since ----                
     D RC              C                   Const(x'000102030B0C0D0E0F1011121316-                
     D                                     18191C1D1E1F2526272D2E2F32373C3D3F')                 
      * Blank characters to replace RC with in Xlate. 31 of them.           ----                
     D BC              C                   Const(                                               
     D                                     '                               ')                   

And, in your code...

    MyText = %XLate(RC:BC,MyText);