Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to exclude spaces/ special characters while checking for an alphanumeric key

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

amy_king
Active Contributor
0 Kudos

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...

  • ^ : start of the checked string
  • [ : start of a character group
  • a-z : any lowercase letter
  • A-Z : any uppercase letter
  • 0-9 : any digit
  • ] : end of a character group
  • * : zero or more occurrences of the preceding characters
  • $ : end of the checked string

Cheers,

Amy

4 REPLIES 4

amy_king
Active Contributor
0 Kudos

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...

  • ^ : start of the checked string
  • [ : start of a character group
  • a-z : any lowercase letter
  • A-Z : any uppercase letter
  • 0-9 : any digit
  • ] : end of a character group
  • * : zero or more occurrences of the preceding characters
  • $ : end of the checked string

Cheers,

Amy

Former Member
0 Kudos

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

0 Kudos

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

amy_king
Active Contributor
0 Kudos

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