2023 Aug 21 10:50 AM
Hi,
I'm trying to write a code where it will show an error message when the user entered an invalid email address.
Correct format should be lastname.firstname@xyz.com. Does someone know how? Will appreciate it.
2023 Aug 21 10:56 AM
Hi,
take a look at cl_bcs_email_address=>validate().
It validates an address according to RFC 5322 and also gives the parts back.
Best regards,
Holm
2023 Aug 21 11:53 AM
You can also add your own check to this class with BAdI BADI_BCS_EMAIL_ADDRESS (read the linked note first)
2023 Aug 21 11:08 AM
Hi nathanlopez,
https://www.stechies.com/abap-email-validation-check--exists-not-required-sequenc/
Thank You,
Suggu Sandeep.
2023 Aug 21 12:07 PM
what about using regex?
DATA(lastname) = 'Doe'.
DATA(firstname) = 'John'.
DATA(email_input_correct) = 'Doe.John@xyz.com'.
DATA(email_input_false) = 'Doe.Johnny@xyz.com'.
" Construct the regex pattern dynamically based on the entered firstname and lastname
DATA(regex) = |^{ lastname }\\.{ firstname }@xyz\\.com$|.
DATA(matcher_correct) = cl_abap_matcher=>create( pattern = regex
text = email_input_correct
ignore_case = abap_true ).
DATA(matcher_false) = cl_abap_matcher=>create( pattern = regex
text = email_input_false
ignore_case = abap_true ).
IF matcher_correct->find_all( ) is not INITIAL.
WRITE: |Valid email format for { email_input_correct }|.NEW-LINE.
ENDIF.
IF matcher_false->find_all( ) is INITIAL.
WRITE: 'Invalid email format. It should be lastname.firstname@xyz.com'.
ENDIF.
2023 Aug 21 12:25 PM
Hi
Use this regex
DATA: go_regex TYPE REF TO cl_abap_regex,
go_matcher TYPE REF TO cl_abap_matcher,
go_match TYPE c LENGTH 1,
gv_msg TYPE string.
PARAMETERS: p_email TYPE ad_smtpadr.
START-OF-SELECTION.
CREATE OBJECT go_regex
EXPORTING
pattern = '\w+(\.\w+)*@(\w+\.)+(\w{2,4})'
ignore_case = abap_true.
go_matcher = go_regex->create_matcher( text = p_email ).
IF go_matcher->match( ) IS INITIAL.
gv_msg = 'Email address is invalid'.
ELSE.
gv_msg = 'Email address is valid'.
ENDIF.
MESSAGE gv_msg TYPE 'I'.
Thanks
Tarun