cancel
Showing results for 
Search instead for 
Did you mean: 

Validate Input-Field for Phone-Number

Former Member
0 Kudos

Hello Everybody,

i stuck in a little issue: My webdynpro has a field to allow users to input phone-numbers.

Finally (bevore writing the input into my db-table) i like to validate if the made input contains only numbers.

regarding to i trief the following.


        DATA lv_tst_string(15) TYPE c.
*      Check if the entered value is numeric
        lv_tst_string = '0123456789'.
        IF ls_customer_mod-telefone CO lv_tst_string.
          " Then raise a message
*         get message manager
          DATA lo_message_manager    TYPE REF TO if_wd_message_manager.
          lo_api_controller ?= wd_this->wd_get_api( ).
          CALL METHOD lo_api_controller->get_message_manager
            RECEIVING
              message_manager = lo_message_manager.
*         report message
          CALL METHOD lo_message_manager->report_error_message
            EXPORTING
              message_text = 'möp'.
        ENDIF.

But, no matter what i enter as phone number, the message is never thrown and the insert ever performed.

What did i miss?

Best regards

Philipp

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Thanks for your suggestions!

Actually not the ouput is overwritten, but the if condition doesnt work.

IF ls_customer_mod-telefone CO '0123456789'.
   ...
ELSE.
   ...
ENDIF.

This always jumps into the else-block, no matter what i typed in. I feel bit confused

Regarding the second idea: I chose type characters because i thought num / i does not allow leading "0" like 0172938478. Or am i wrong?

Best regards

Philipp

Edited by: Philipp.Heinemann on May 19, 2010 11:57 AM

Former Member
0 Kudos

IF ls_customer_mod-telefone CO '0123456789'.

...

ELSE.

...

ENDIF.

Try to use the statement as below

IF ls_customer_mod-telefone CN '0123456789'. " change CO to CN.

As,

CO -> Contains Only: True, if operand1 only contains characters from operand2. Upper/lower case and trailing blanks are taken into account for both operands. If operand2 is of type string and is initial, then the logical expression is false, unless operand1 is also of type string and is initial, in which case the logical expression is always true. If the result of the comparison is negative, sy-fdpos contains the offset of the first character in operand1, that is not contained in operand2. If the result of the comparison is positive, sy-fdpos contains the length of operand1.

CN -> Contains Not Only; True if a logical expression with CO is false, that is, if operand1 contains not only characters from operand2. sy-fdpos is set in the same way as for CO. If the comparison is true, sy-fdpos contains the offset of the first character in operand1 that is not contained in operand2. If the comparison is false, sy-fdpos contains the length of operand1.

Hope it helps you.

Former Member
0 Kudos

good explanation of CO and CN!

unfortunatelly, after the change to CN it always jumps into the then-block (for "393" as well as "dfjkd").

Edited by: Philipp.Heinemann on May 19, 2010 12:18 PM

Former Member
0 Kudos

Include a space with the digits '0123456789<space>.

See if it works.

Former Member
0 Kudos

Surprisingly adding a space in the condition works.

IF ls_customer_mod-telefone CN '0123456789 '.

Unfortunatelly, "928 938 8" are now valid inputs, which is okay in this stage of the the program.

But is there any way to kind of "trim" inputs. For example to avoid leading spaces in a name-field?

Former Member
0 Kudos

Surprisingly adding a space in the condition works.

IF ls_customer_mod-telefone CN '0123456789 '.

Unfortunatelly, "928 938 8" are now valid inputs, which is okay in this stage of the the program.

But is there any way to kind of "trim" inputs. For example to avoid leading spaces in a name-field?

Former Member
0 Kudos

Have you tried out the CONDENSE statement to avoid leading spaces in a name-field?

For E.g. CONDENSE NAME_FIELD.

Former Member
0 Kudos

yes, i tried to apply CONDENSE in the WDDOBEFOREACTION on fields of the context-node, but it seems to have no effect at all.

Answers (4)

Answers (4)

preethi_santhanam
Participant
0 Kudos

Hi Phillipp,

You could try and use the regular expression. I have used this and it works. You could try a pattern as:

 ^[1-9]\d{2}-[1-9]\d{2}-\d{4}$  

Let me know if you need more clarity.

Regards,

Preethi.

Former Member
0 Kudos

The condense did work, but i didn't set the attributes back into the context node - bad mistake

Now the condese absolutely solves my problem.

Thank you for the regex-hint as well. For more complex checks this is defenitely the tool of choice, but for my purpose the condense fits best.

Thank you guys!

Former Member
0 Kudos

Hi,

Either change the type of Telefone to Num or I, then automatically it will whether all digits entered are numbers or you can check via code like this :

if lv_ca_no CN '0123456789 '   .  < Add one space also after 9 >
<Throw error message>
Endif.

ChrisPaine
Active Contributor
0 Kudos

Reporting a message to the message manager is not like doing a

message e016(rp) with 'blah'.

in the PAI of a screen - it doesn't stop the processing.

With this in mind you might want to re-write the code above so that it is an if - else - endif construct rather than just an if -message - endif type construct.

Be aware also of the lifetime of the message you generate - if you are navigating away from the current component and the message was raised against the current component, you won't see that message on the screen. You can deal with this by using a "umbrella" component - or rather than having to do that yourself - use the FPM framework which provides such functionality itself!

Former Member
0 Kudos

Hello,

Instead of using method report_error_message, call method "raise_t100_error" and store your error message in a message class using SE91 transaction. pass msgid = message class name, msgno = message no and msgty = 'E'. It should work.

it seems that in your case, the code is reaching to the error message point and it is moving forward and your error message is over written with success message and process is completed. if it does not work, use statement "exit" just after the method report_error_message in your code.

Thanks

Vishal Kapoor