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 10 numeric fields - any better approach?

Former Member
0 Likes
654

Hello All,

I've an internal table with 15 fields. First 5 fields are all text fields and the next 10 fields are of type DMBTR. I have to validate each of these fields. Currently I've coded as follows:

data: begin of itab1 occurs 0,

text1 type char4,

text2 type char2,

text3 type char10,

text4 type char6,

text5 type char4,

amt1 type dmbtr,

amt2 type dmbtr,

amt3 type dmbtr,

amt4 type dmbtr,

amt5 type dmbtr,

amt6 type dmbtr,

amt7 type dmbtr,

amt8 type dmbtr,

amt9 type dmbtr,

amt10 type dmbtr,

end of itab1.

perform some steps and

collect data into itab1.

loop at itab1.

if itab1-amt1 CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or

itab1-amt1 CA 'abcdefghijklmnopqrstuvwxyz' or

itab1-amt1 CA '~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?'.

write:/ 'error'.

endif.

if itab1-amt2 CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or

itab1-amt2 CA 'abcdefghijklmnopqrstuvwxyz' or

itab1-amt2 CA '~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?'.

write:/ 'error'.

endif.

...

...

...

if itab1-amt10 CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or

itab1-amt10 CA 'abcdefghijklmnopqrstuvwxyz' or

itab1-amt10 CA '~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?'.

write:/ 'error'.

endif.

endloop.

Do we have a better approach to do this, instead of repeating the same step for 10 fields?

Thanks in advance.

Siri

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
622

or using field symbols


data: fieldname(11) type c value 'ITAB1-AMT',
         idx(2) type c.

field-symbls <field> type any.

loop at itab1.
   do 10 times.
      write sy-index to idx. "' 1', ' 2'...'10'
      condense idx. "'1', '2'...'10'
      concatenate fieldname idx into fieldname. "ITAB-AMT1, ITAB-AMT2 ...
       
      assign (fieldname) to <field>. "now <field> stores appropriate value
      if <field> CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or 
         <field> CA 'abcdefghijklmnopqrstuvwxyz' or
         <field> CA '~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?'.
        write: / 'error'.
      endif.
   enddo.
endloop.

Regards

Marcin

3 REPLIES 3
Read only

guilherme_frisoni
Contributor
0 Likes
622

Hi,

You could Macros instead:


DEFINE check_number.
  if not itab1-amt&1 co '0123456789'.
    write:/ 'error'.
  endif.
END-OF-DEFINITION.

check_number: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

BUT, as you declared your fields as type of DMBTR, and this is a CURRENCY type, if you try to assign a character that is not a number, should give you a Dump.

Regards,

Frisoni

Read only

MarcinPciak
Active Contributor
0 Likes
623

or using field symbols


data: fieldname(11) type c value 'ITAB1-AMT',
         idx(2) type c.

field-symbls <field> type any.

loop at itab1.
   do 10 times.
      write sy-index to idx. "' 1', ' 2'...'10'
      condense idx. "'1', '2'...'10'
      concatenate fieldname idx into fieldname. "ITAB-AMT1, ITAB-AMT2 ...
       
      assign (fieldname) to <field>. "now <field> stores appropriate value
      if <field> CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or 
         <field> CA 'abcdefghijklmnopqrstuvwxyz' or
         <field> CA '~`!@#$%^&*()-_=+[{]}\|;:'",<.>/?'.
        write: / 'error'.
      endif.
   enddo.
endloop.

Regards

Marcin

Read only

Former Member
0 Likes
622

Thank You Marcin Pciak and Frisoni

It worked. I've awarded points to you both.

Siri