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

how to validate a numeric field using regex

Former Member
0 Likes
12,159

hello,

My requirement is to validate a field for alphabets and special characters.So the field should allow only numerics and spaces.I am using

FIND FIRST OCCURRENCE OF REGEX `[[:punct:]]` IN lv_1.

       IF sy-subrc = 0. "Special character is found

         MESSAGE 'You can only enter numeric characters ' TYPE 'E'.

       ENDIF.

       FIND FIRST OCCURRENCE OF REGEX '[ [:alpha:] ]' IN lv_1.

       IF sy-subrc = 0. "Special character is found

         MESSAGE 'You can only enter numeric characters ' TYPE 'E'.

       ENDIF.

It is working fine when special characters or alphabets are entered but it is throwing error for even spaces at second find statement. I know there is an explicit way to validate spaces by using [[:space:]].But why is it throwing error at second statement.Any replies are appreciated thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
6,163

Hi Maverick,

Use this regular expression.

FIND REGEX '[^[:digit:]|^[:space:]] IN lv_1.

IF sy-subrc = 0.

   ** Invalid

ELSE.

  ** Valid.

Regards,

Satish D R

hello,

My requirement is to validate a field for alphabets and special characters.So the field should allow only numerics and spaces.I am using

FIND FIRST OCCURRENCE OF REGEX `[[:punct:]]` IN lv_1.

       IF sy-subrc = 0. "Special character is found

         MESSAGE 'You can only enter numeric characters ' TYPE 'E'.

       ENDIF.

       FIND FIRST OCCURRENCE OF REGEX '[ [:alpha:] ]' IN lv_1.

       IF sy-subrc = 0. "Special character is found

         MESSAGE 'You can only enter numeric characters ' TYPE 'E'.

       ENDIF.

It is working fine when special characters or alphabets are entered but it is throwing error for even spaces at second find statement. I know there is an explicit way to validate spaces by using [[:space:]].But why is it throwing error at second statement.Any replies are appreciated thanks.

12 REPLIES 12
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
6,163

What is the data type of the variable LV_1?

Read only

Former Member
0 Likes
6,163

string suhas.

Read only

Former Member
0 Likes
6,163

Hi Maverik,

I would suggest that you rather use the RegEx matcher class:

  DATA:

  go_matcher TYPE REF TO cl_abap_matcher.


  go_matcher = cl_abap_matcher=>create( pattern     = '^[0-9 ]+$'

                                        ignore_case = abap_true

                                        text        = 'Text to be checked' ).

  IF go_matcher->matches( ) = abap_true.

     " ... do something ...

   ENDIF.

This is far more flexible since you can use RegEx groups to isolate certain elements of the original text and other advanced regular expression features.

If you want to try out various regular expressions beforehand you can use the program DEMO_REGEX_TOY (launched via transaction SE38) which is particularly useful since ABAP RegEx support only a subset of the PCRE RegEx syntax.

Hope that helps.

Regards,

  Chris

Read only

RaymondGiuseppi
Active Contributor
0 Likes
6,163

I usually use part of this generic expression : ^[-+]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$ to check validity of numeric values.

Regards,

Raymond

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
6,163

Isn't it too complicated to check if a value is numeric? K.I.S.S, eh?

- Suhas

Read only

0 Likes
6,163

Oh, and I did not even post the multiple-country version...

(Usually a simple TRY/MOVE/CATCH/ENDTRY do the job too)

Regards,

Raymond

Read only

0 Likes
6,163

Hi Raymond,

I think Maverick's question is rather directed towards a general solution on how to validate standard numeric input (including spaces!).

To do it the SAP-way you could also simply resort to the function module CATS_NUMERIC_INPUT_CHECK which basicaly does the trick (except for the spaces)

BTW:

The best term (that I know of) to discern all possible numerical formats is the PCRE expression:

  • [-+]?(?:\b[0-9]+(?:\.[0-9]*)?|\.[0-9]+\b)(?:[eE][-+]?[0-9]+\b)?

(this is definitely not KISS - and does neither catch spaces nor works here since ABAP RegEx engine does not support grouping without backreferences)

Regards,

  Chris


Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
6,163

To do it the SAP-way you could also simply resort to the function module CATS_NUMERIC_INPUT_CHECK which basicaly does the trick (except for the spaces)

Or may be define the variable as numeric/integer   Of course this won't cater to the spaces problem.

[-+]?(?:\b[0-9]+(?:\.[0-9]*)?|\.[0-9]+\b)(?:[eE][-+]?[0-9]+\b)?

In addition to matching numeric input, does this code send the Starship Enterprise on a Warp drive

BR,

Suhas

Read only

0 Likes
6,163

No, unfortunately this only activates Impulse Drive - in order to activate Warp Drive you have to set the RegEx option 'dot matches newlines' to capture all numerics in the Universe ...

Read only

prkash_s
Participant
Read only

Former Member
0 Likes
6,164

Hi Maverick,

Use this regular expression.

FIND REGEX '[^[:digit:]|^[:space:]] IN lv_1.

IF sy-subrc = 0.

   ** Invalid

ELSE.

  ** Valid.

Regards,

Satish D R

Read only

Former Member
0 Likes
6,161

Thanks ppl for your prompt replies, i got this solved.Space validation wasnt needed, but [:alpha:]expression is considering space as a char.So used find regex digit space and it solved the issue.Thanks again.