Application Development 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: 

Invalid Email Address

0 Kudos
661

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.

5 REPLIES 5

holm
Participant
577

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

577

You can also add your own check to this class with BAdI BADI_BCS_EMAIL_ADDRESS (read the linked note first)

SugguSandeep
Contributor
0 Kudos
577

Marian_Zeis
Active Contributor
0 Kudos
577

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.

TarunTakshak
Participant
0 Kudos
577

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