780 pts.
 Search a character in a string in rpgle
Hi I have a character filed and and i dont want to allow special character and digits


how can i solve

My code is

Dscan             s             50    inz('0123456789+        
D                                       `~!@#$%¢&*()_+|=-¦¬}{ +
D                                       :/.,?:"><.,')

d@scan            s              5u 0                       
dstr              s             10    inz('am$%@shukl')     
                                                            
c*                  eval      scan=%trimr(scan)             
c                   eval      @scan=%check(scan :%trim(str))
c     @scan         dsply                                   
c                   eval      @scan=%scan(str : scan)       
c*                  eval      @scan=%check(str :%trim(scan))
c     @scan         dsply                                   
c                   return                                             
ASKED: Jan 24, 2012  6:49 AM GMT
UPDATED: February 28, 2012  11:11:28 AM GMT
66,990 pts.
  Help
 Approved Answer - Chosen by 9783444184 (Question Asker)

If all you want to do is check to see if you only have letters, no special characters or digits), then simply check for letters:

D string1 s 50a varying
D inz( 'ABCDEFG1:' )
D string2 s 50a varying
D inz( 'ABCDEFGHI' )

D chars C 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

/free

if 0 <> %check( chars : string1 ) ;
dsply 'string1 has junk' ;
endif ;

if 0 <> %check( chars : string2 ) ;
dsply 'string2 has junk' ;
endif ;

*inlr = *on ;
return ;

/end-free

That has two string variables -- string1 and string2. And it has a constant that contains only letters -- chars.

It first checks string1 to see if there's anything that doesn't match between chars and string1. If it finds any position in string1 that doesn't match, the %check() function will return the position of the mismatched character. So if it returns anything but zero, it will display the message 'string1 has junk'.

Then it checks string2. If it finds anything but letters, it will display the message 'string2 has junk'.

Because string1 has "1:" at the end, you should get the 'string1 has junk'. And because string2 only has letters, you won't get a message for string2.

Tom
ANSWERED:  Jan 24, 2012  10:28 PM (GMT)  by TomLiotta   66,990 pts.

 
Answer Wiki:
Last Wiki Answer Submitted:  Feb 7, 2012  2:47 PM (GMT)  by  MTidmarsh   150 pts.
Latest Answer Wiki Contributors:  Prettyshina   1,330 pts.
To see other answers submitted to the Answer Wiki View Answer History.
Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _




 

i dont want to allow special character and digits

What do you want to do if you find them?

Tom

 66,990 pts.

 

if i found my screen field then validate it if there any other option for it please tel me

 780 pts.

 

For a simple validation test, translate any forbidden characters (to anything else, doesn’t matter as long as each translated character is different than the forbidden one it replaces) and if the translated string is not equal to the compare string, then there are forbidden characters in the compare string

d goodChar        s              3a   inz(’___’)
d forbidden       s              3a   inz(’1 $’)
d compareStr      s             50a   inz(’ABCDEFG1HIJK LMN$OP’)                   

 /free
     if compareStr <> %xlate(forbidden:goodChar:compareStr);
       // forbidden characters in field
     endif;
     *inlr = *on;
 /end-free
 5,555 pts.

If all you want to do is check to see if you only have letters, no special characters or digits), then simply check for letters:

     D string1         s             50a   varying
     D                                     inz( ‘ABCDEFG1:’ )
     D string2         s             50a   varying
     D                                     inz( ‘ABCDEFGHI’ )

     D chars           C                   ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’

      /free

       if 0 <> %check( chars : string1 ) ;
          dsply ’string1 has junk’ ;
       endif ;

       if 0 <> %check( chars : string2 ) ;
          dsply ’string2 has junk’ ;
       endif ;

       *inlr = *on ;
       return ;

      /end-free

That has two string variables — string1 and string2. And it has a constant that contains only letters — chars.

It first checks string1 to see if there’s anything that doesn’t match between chars and string1. If it finds any position in string1 that doesn’t match, the %check() function will return the position of the mismatched character. So if it returns anything but zero, it will display the message ’string1 has junk’.

Then it checks string2. If it finds anything but letters, it will display the message ’string2 has junk’.

Because string1 has “1:” at the end, you should get the ’string1 has junk’. And because string2 only has letters, you won’t get a message for string2.

Tom

 66,990 pts.

 

Hi TomLiotta
Thanks for your valuable example

 780 pts.