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

Validating Numbers

Former Member
0 Likes
1,388

Hi,

I have to validate a field for numbers. Its an Amount field. I am taking the input in a Character field and then moving it to an amount field in the code.

I have done the validation for entering characters and throwing error message in this case.

How do i do the validation if something like '1..00' is entered or '1.0.2.3.4.' is entered?

Quick answers will be rewarded.

Thanks,

Rashmi.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,144

Divide the number by 1 and check if it equal to the same number and give a try..

v_var =  v_amount / 1.

if v_var = v_amount.
else.
 *give error message that it is not anumber
endif.

6 REPLIES 6
Read only

Former Member
0 Likes
1,145

Divide the number by 1 and check if it equal to the same number and give a try..

v_var =  v_amount / 1.

if v_var = v_amount.
else.
 *give error message that it is not anumber
endif.

Read only

Former Member
0 Likes
1,144

data p1(8) type c '1.2.3.4'.

data p2 type p decimal 2.

<b>

try.

p2 = p1.

catch cx_root.

mesage ' Invalid input'.

endtry.</b>

Read only

Former Member
0 Likes
1,144

Hi Rashmi,

Here is one idea:

1) Take the number into temp variable l_num.

2) use FIND '.' IN l_num MATCH OFFSET off.

3) then using the offset value in 'off' take the remaining string from there oneards.

4) l_num2 = l_num+off.

5) now apply same FIND on l_num2.

6) if another '.' is found then the input was wrong like 1..0 or 1.2.

Both the condition metioned by u can be validated by this logic.

Regards,

Vivek Kute.

Read only

Former Member
0 Likes
1,144

Hi Rasmi,

you can use CATCH to validate the input...

Please see the following codes:

DATA int TYPE i.

CATCH SYSTEM-EXCEPTIONS conversion_errors = 1.

MOVE 'abc' TO int.

ENDCATCH.

IF sy-subrc = 1.

MESSAGE i398(00) WITH 'Conversion Error'.

ENDIF.

Regards,

Hendy

Read only

Former Member
0 Likes
1,144

Hi,

I hope the following code will be helpful.

Please do validation in character field only and move to numeric fileds.

DATA:

c_numeric TYPE char69

VALUE '12345 .67890'. " Alphanumc Group

IF lv_amount CO c_numeric.

find '.' in lv_amount MATCH LENGTH mlen.

if mlen gT 1.

Message 'Invalid amount value'.

endif.

  • --- define your code here.

ELSE.

Message 'Invalid amount value'.

ENDIF.

Regards

Bhupal Reddy

Message was edited by:

Bhupal Reddy Vendidandi

Read only

Former Member
0 Likes
1,144

Hi All,

Thanks a lot for your answers.

I am now catching the exception and giving the message.

Thanks,

Rashmi.