10-02-2012 6:55 PM
Hi Experts,
I have a case where I need to check for an alphanumeric key number in the first few characters of a field. However, there is a possiblity of spaces or special characters being present. I have used 'CA' operator to check for numbers and alphabets. However, these two checks alone do not deal with spaces and spaces/ special characters are likely included in the output.
"
IF ( operator1(4) CA '%0123456789%' " Check for numbers
AND operator1(4) CA sy-abcde ). " Check for alphabets
DO something.
END IF.
"
Is there an easier way to add an additional check for getting rid of spaces/ sp characters, apart from counting string lengths?
Thanks in advance,
PKD
10-02-2012 8:26 PM
Hi Prajakt,
You could use a regular expression to check that your variable contains only alphabetic characters and digits and nothing else.
FIND REGEX '^[a-zA-Z0-9]*$' IN operator1(4).
IF sy-subrc IS INITIAL.
do something.
ENDIF.
The breakdown of the expression is...
Cheers,
Amy
10-02-2012 8:26 PM
Hi Prajakt,
You could use a regular expression to check that your variable contains only alphabetic characters and digits and nothing else.
FIND REGEX '^[a-zA-Z0-9]*$' IN operator1(4).
IF sy-subrc IS INITIAL.
do something.
ENDIF.
The breakdown of the expression is...
Cheers,
Amy
10-03-2012 11:46 PM
Hi Amy,
Appreciate your reply.
Just one question -
When I use it for a validating a 2 character alphanumeric, it fails.
Code snippet:
FIND REGEX '^[a-zA-Z0-9]*$' IN operand+4(2). [where, operand = KEY and operand+4(2) = AT/ ON]
The sy-subrc check here gives 0 but it should not be the case as there are no numbers in this and i want to check for alphanumeric.
Rather when I use it to test a complete alphabetic operand, then also the sy-subrc value is 0. I think it is because ' * ' checks for '0' or more occurences. Hence, it will give success when I check with an alphabetic operand.
I am looking for something which will:
1. check that my variable contains only alphabetic characters and digits and nothing else
2. check that atleast one number is present in the variable be (which make it alphanumeric).
Can you please suggest?
Thanks in advance,
Prajakt
10-04-2012 6:06 AM
Hi,
Try this.
IF V_DATA CO '0123456789'.
WRITE: 'NUMERIC'.
ELSEIF V_DATA CO SYABCDE.
WRITE: 'ALPHABETS ONLY'.
ELSE.
WRITE: 'ALPHANUMERIC'.
ENDIF.
Source:http://scn.sap.com/thread/1209522
regards,
Nandha
10-04-2012 2:50 PM
Hi Prajakt,
1. check that my variable contains only alphabetic characters and digits and nothing else.
2. check that at least one number is present in the variable be (which make it alphanumeric).
In this case, you can rework the regex slightly. The + sign below means 1..n occurrences of the digits group.
^[a-zA-Z]*[0-9]+[a-zA-Z]*$
Cheers,
Amy