Application Development and Automation 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: 
Read only

Validate Alpha Numeric Variable .

Former Member
0 Likes
24,960

i have a company variable and i need to validate it  with all abcde...z and 1234567890 .

and i know only one procedure to do this :

if v_comp co 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789''

is there any other way to do in void hard code ??

36 REPLIES 36
Read only

Former Member
0 Likes
16,604

Hello,

For the A to Z part u can use the system variable SY-ABCDE.

data g_var type string.

g_var = 'A0131V'.

IF g_var ca sy-abcde and g_var ca '0123456789'.

   write 'Alphanumeric'.

ENDIF.

Thanks and Kind Regards,

Yovish.

Read only

0 Likes
16,604

can you come up with example that includes 1234567890

Read only

0 Likes
16,604

Is this example you are refering to?

A1234567890

Result : Alphanumeric

Read only

0 Likes
16,604

Hi Praveen Bindla

Try like this

PARAMETERS : p_data TYPE string.

IF p_data CO '0123456789'.
   WRITE: 'NUMERIC ONLY'.
ELSEIF p_data CO sy-abcde.
   WRITE: 'ALPHABETS ONLY'.
ELSE.
   WRITE: 'ALPHANUMERIC'.
ENDIF.

Read only

0 Likes
16,604

The last else statement can include 'space' special symbol ?

Read only

Former Member
0 Likes
16,604

Hi,

  Please check this..

You can use the RegEx '[[:punct:]]' to achieve this requirement:

  1. PARAMETERS: p_data TYPE c LENGTH 10 OBLIGATORY. 
  2.  
  3. AT SELECTION-SCREEN ON p_data. 
  4.    FIND FIRST OCCURRENCE OF REGEX `[[:punct:]]` IN p_data. 
  5.    IF sy-subrc = 0. "Special character is found 
  6.      MESSAGE 'Please input alphanumeric values only' TYPE 'E'. 
  7.    ENDIF. 
Read only

0 Likes
16,604

Hi kusuma ,

am in a class method , the code is in method ,

can u tell with an example .

Read only

Former Member
0 Likes
16,604

Regex can be used. ALNUM stands for alpha-numeric character. Sy-subrc will be zero if string has only alpha-numeric characters.

FIND REGEX '^[[:alnum:]]+$' IN v_comp.

Message was edited by: Manish Kumar

Read only

0 Likes
16,604

i have given your same code and executed and given 'a234' as input , but sy-subrc is '4' ..

Read only

0 Likes
16,604

The code works for string type.

Your variable is probably of char type, which means trailing spaces, which means you need to search for alpha-numeric, followed by any number of spaces.

DATA v_comp TYPE char10 VALUE 'a123'.
DATA v_comp_str TYPE string VALUE 'a123'.

FIND REGEX '^[[:alnum:]]+$' IN v_comp_str.
WRITE:/ sy-subrc.
FIND REGEX '^[[:alnum:]]+$' IN v_comp.
WRITE:/ sy-subrc.
FIND REGEX '^[[:alnum:]]+\s*$' IN v_comp.
WRITE:/ sy-subrc.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
16,604

FIND REGEX '^[[:alnum:]]+$' IN v_comp.

Why do you negate the alpha numeric([[:alnum:]]) character set? The OP wants to check if the input string is alpha-numeric.

I'm a novice in RegEx but i think the following construct looks for aplhanumeric correctly -

Message was edited by: Suhas Saha

Stupid me! Moday afternoon blues

BR,

Suhas

Read only

0 Likes
16,604

Hi Suhas

When ^ is used with character set [ ], it works as negation.

When ^ is used at start of pattern, it means start of line.

$ at end of pattern means end of line.

Instead of matching pattern with any substring, I am matching the pattern with full input string by specifying start and end of line.

So, [[:alnum:]] matches any alpha-numeric character, and FIND REGEX means find first occurrence. Your construct with get successful match as 'a', irrespective of subsequent characters. For example gv_compare = 'a123%' will get positive match.

I have checked for 'contains only' by specifying ^ and $.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
16,604

Please check the update

Read only

0 Likes
16,604

Saw the update right after hitting save button.

Too bad SCN does not show warning that stuff you are replying to has changed.

GMail does alert the user when new email in same thread arrives while drafting a reply.

Even better would be show popup to user saying, 'Hey, you are too slow. 4 new replies came in from the moment you started drafting this lengthy reply ', and one of the buttons would say 'Continue anyways, I don't care'.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
16,604

I should have checked once before replying I don't use RegEx regularly & hence tend to forget the syntax.

- Suhas

Read only

0 Likes
16,604

Hello manish ,

                it worked . good solution man .

Thanks .

Read only

Former Member
0 Likes
16,604

Hi techies,

Am concatenating sy abcde  and '0123456789' to var .

And using 'co' to compare it with company code.

So as of now it's comparing correctly.

But manish can you be clear in explaining Regex concept .

Am very new to concept what said.

I want to use your Regex way..

Thanks...

Read only

0 Likes
16,604

Hi,


Check program 'DEMO_REGEX_TOY' should help you get a fair idea and play around with REGEX.

Cheers,

Arindam

Read only

0 Likes
16,604

This message was moderated.

Read only

0 Likes
16,604

Hi ,

I wonder why this solution that I had posted before was rejected.

If you want to acheive this using REGEX command, use one of the following statement. Both will work fine.


FIND REGEX '[^[:alnum:]]' IN input.

or

FIND REGEX '[^A-Za-z0-9]+' IN input.

IF sy-subrc = 0.
   WRITE 😕 'Not alphanumeric ' .
ELSE.
   WRITE 😕 'Alphanumeric'.
ENDIF.

Read only

0 Likes
16,604

if sy-subrc = 0 . then it should be alpha neumaric right ?

and if sy-subrc neq 0 then its not alpha neumaric ?

Read only

0 Likes
16,604

No, its the way I have written.

If sy-subrc = 0 , it means that it has come across some character that is not alphanumeric.

if sy-subre ne 0, it means it has not found any character that is not alphanumeric. Hence its an alphanumeric string.

Read only

0 Likes
16,604

Hi sushmitha ,

i have checked ur code , but it is always showing the sy=subrc as 0

either for alpha neumaric or non alpha neumaric .

i have given input as 'A1234' and '@1234' for both cases it was giving sy-subrc = 0 .

FYI : my input is char type .

Read only

0 Likes
16,604

Well, this is my complete code. And its always working correctly for me whichever statement I use. FIND REGEX '[^[:alnum:]]' IN input or  FIND REGEX '[^A-Za-z0-9]+' IN input.


DATA : input TYPE  string.

input = 'a234a%##y4'.

WRITE 😕 input.
FIND REGEX '[^[:alnum:]]' IN input.
*FIND REGEX '[^A-Za-z0-9]+' IN input.

IF sy-subrc = 0.
   WRITE 😕 'Not alphanumeric ' .
ELSE.
   WRITE 😕 'Alphanumeric'.
ENDIF.


Output on changing the value of input

Note : Data type is string. 

Read only

0 Likes
16,604

Instead of Char Type use String Type .

PARAMETERS: input type string .

FIND REGEX '[^A-Za-z0-9]+' IN input.

IF sy-subrc = 0.

    WRITE :/ 'Not alphanumeric ' .

ELSE.

    WRITE :/ 'Alphanumeric'.

ENDIF.

Regard's

Smruti

Read only

0 Likes
16,604

Cheers ,

urs is working only for string type .

but what if my input is char type ?

Read only

0 Likes
16,604

If you are using char type, then there is a small change to the regex.

FIND REGEX '[^[:alnum:]\s]' IN input.


or

FIND REGEX '[^A-Za-z0-9\s]+' IN input.


Read only

0 Likes
16,604

Again cheers sushmitha ,

its working .

Read only

0 Likes
16,604

if u dont mind can you explain me .

see REGEX checks for a-z or 0-9 in input then if it finds then sysubrc should 0 .

and if it finds special character then it should ne 0 .

but in ur case its vice versa ?

Read only

0 Likes
16,605

'[^A-Za-z0-9]+'

This character ^ is the negation of a value set.

So when ^ is added to the expression, it will find a character that is not in A-Z, a-z, 0-9.

The search is for any character outside this set. So when it comes across one such character, sy-subrc becomes 0 and you know that its not alphanumeric.

If sy-subrc ne 0, it means the regex could not find any character that is not in the given set, hence its purely alphanumeric.

Hope its clear now. 🙂



Read only

0 Likes
16,605

Perfect and thanks..

Read only

0 Likes
14,036

It is giving sy-subrc = 4 for both alpahnumeric and non-alphanumeric values.

Read only

Former Member
0 Likes
16,605

Hi,

Check 'NUMERIC_CHECK' function

Read only

Former Member
0 Likes
16,605

You can use the FM CONVERSION_EXIT_ALFAN_INPUT.

This would give an error if any special character is entered, only alphabets, numbers and underscore is allowed.

Read only

0 Likes
16,605

Susmitha,

But he will got error by using this FM if his string or variable contains any space or invalid char.

Regards

Vivek

Read only

Former Member
0 Likes
16,605

Hello manish ,

i checked your solution , it worked .

Thanks ..