Friday, November 23, 2007

Check Length and Alpha Numeric Variable in sap abap

* Program to Check Length and Alpha Numeric Variable
*
* Is there any simple way or FM to check the string contains other
* than alphanumeric(A-Z and 0-9) and give an error message.
*
* How to check the length size of a variable?
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
* http://www.sap-img.com
*
REPORT ZCHECK_ALPHA_NUMERIC.

* Declare the variable

* For Length
data: serial_length type i.

* For Alpha numeric
data: str type string.
data: valid_characters type string.

* Fill in those valid characters you need to check
concatenate '0123456789' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz' into valid_characters.

* User Input
parameters testchar(10) default '12345abc'.

* Get User Input
str = testchar.

* The Checks
if str co valid_characters.
write: / str, 'Characters are OK'.
else.
write: / str, 'Characters are NOT OK'.
endif.

* Check if length is equal to 10 characters
serial_length = strlen( str ).
if serial_length <> 10.
write: / str, 'Not 10 Digit'.
endif.

*-- End Program

No comments:

Blog Archive