2013 Jul 17 2:03 AM
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.
2013 Jul 17 12:53 PM
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.
2013 Jul 17 7:46 AM
2013 Jul 19 3:56 PM
2013 Jul 17 9:14 AM
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
2013 Jul 17 9:43 AM
2013 Jul 17 9:48 AM
Isn't it too complicated to check if a value is numeric? K.I.S.S, eh?
- Suhas
2013 Jul 17 10:05 AM
2013 Jul 17 10:13 AM
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:
(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
2013 Jul 17 10:30 AM
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
2013 Jul 17 10:43 AM
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 ...
2013 Jul 17 12:28 PM
Hi ,
Please Refer this Document
http://www.patelashish.com/wordpress/PDFDocs/Regular%20Expression%20Processing%20in%20ABAP.pdf
2013 Jul 17 12:53 PM
Hi Maverick,
Use this regular expression.
FIND REGEX '[^[:digit:]|^[:space:]] IN lv_1.
IF sy-subrc = 0.
** Invalid
ELSE.
** Valid.
Regards,
Satish D R
2013 Jul 19 7:49 PM
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.
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |