SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

How to validate user input in Web Dynpro with Regex

How to validate user input in Web Dynpro with Regex

With this article you can understand how to validate user input in Web Dynpro with Regex. In case the user enters a combination of small and capital letters from A to Z or spaces,we show a message with the text “Your input is string��� If not, we show a message “Your input is not string”. To show these messages,we use a MessageArea UI element.

Standard program DEMO_REGEX_TOY can be used to test regex expressions, which offer the possibility to test the regex created before using them. By using regex, we can validate the values that the user enters in a Web Dynpro screen.

Regex toy screen (Program DEMO_REGEX_TOY )

How to validate user input in Web Dynpro with Regex

View Layout

How to validate user input in Web Dynpro with Regex

As we can see, in this case we have entered a MessageArea UI element in the Group container.This is the place where all the messages will be displayed. When the user presses the button with the id “BTN”,the Framework triggers the event handler method onactioncheck (Listing ).

Checking the data entered by the user method

As we can see, in this case we have entered a MessageArea UI element in the Group container.This is the place where all the messages will be displayed. When the user presses the button with the id “BTN”,the Framework triggers the event handler method onactioncheck (Listing ).

Checking the data entered by the user method


METHOD onactioncheck.

DATA: lv_firstname TYPE string,

lv_pattern TYPE string.

wd_context->get_attribute(EXPORTING name ='FIRSTNAME'

IMPORTING value = lv_firstname).

v_pattern ='[a-zA-Z\s]*'.

CHECK lv_firstname IS NOT INITIAL.

DATA: lr_api_controller TYPE REF TO if_wd_controller,

lr_message_manager TYPE REF TO if_wd_message_manager.

lr_api_controller ?= wd_this->wd_get_api( ).

lr_message_manager = lr_api_controller->get_message_manager( ).

DATA: lt_message_parameters TYPE wdr_name_value_list,

ls_message_parameter LIKE LINE OF lt_message_parameters.

ls_message_parameter-name = 'first_name'.

ls_message_parameter-value = lv_firstname.

APPEND ls_message_parameter TO lt_message_parameters.

F cl_abap_matcher=>matches(pattern = lv_pattern

text = lv_firstname) = abap_true.

lr_message_manager->

report_message(message_text ='Your input &first_name is string'

message_type = 0

params = lt_message_parameters).

ELSE.

lr_message_manager->

report_message(message_text ='Your input &first_name is not string!'

message_type = 2

params = lt_message_parameters).

endif.

ENDMETHOD.

 

By using the static method MATCHES of the CL_ABAP_MATCHER class,we check the value that the user enters in the inputField UI element.In case this value matches our condition specified in the local variable lv_pattern,we use the method REPORT_MESSAGE to display a message of information type. Otherwise,we use the same method to display an error message. Because we want to show to the user whether the value he entered in the input field is a string or not,we use a parameter required to integrate,in the string that he sees on the screen,the value he has just entered.For this purpose, we have used for the method report_message a parameter named “params”.By double-clicking on name of the method report_message,we navigate forward and we can see all the parameters that this method has and the type that the parameters should be. For the “parms” parameter, we need a variable owdr_name_value_list type that is actually a table type with a structure of line type.From this structure, we have used the name(the parameter name,in our case “first_name”)and the value (the value with what we want to replace,at runtime,the parameter name “lv_firstname”).In Chap. 10, we will see more about the message manager and parameters, along with the modality we can display a message into a window, or the modality we can use the ABAP classes to create messages and exceptions. In Fig.the messages are displayed by using the MesageArea UI element.

Runtime with MessageArea UI element

How to validate user input in Web Dynpro with Regex

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in