Register Login

ABAP e-mail Validation, Check @ and '.' Exists Or Not In Required Sequence, No white space

Updated May 18, 2018

Objective:  

This patch of code can be reused in program which sends an e-mail to internet E-mail IDs and where the address validation is required. ECC 6.0 provides a FM “SX_INTERNET_ADDRESS_TO_NORMAL”. 

The address XXX@YYY is shown as valid ID by this FM, which is not correct (.com is missing). Instead this patch of code can be put in a subroutine / Z* FM for such validations.

 Description: 

It validates the e-mail address for the following:

1) ‘@', and '.' exists or not in required sequence.

 2) No white space

3) Name and domain does not contain @ and are not empty

4) At least one and at maximum two periods in domain

Form  VALIDATE_P_MAIL

* Validate e-mail ID on selection screen

* -->P_S_EMAIL_LOW:  E-meil ID entered by user on selection screen

form validate_p_mail  using p_s_email_low.

data: lv_search type sy-fdpos,
lv_length type i,
lv_len_name type i,
lv_len_domain type i,
lv_name(100) type c,
lv_domain(100) type c.

* clear indicator for mail validation which is used to raise error msg
  
clear: wf_mail, lv_search, lv_length, lv_len_name, lv_len_domain,
         
lv_name, lv_domain.

* Calculate the length of the email field.
  
lv_length = strlen( p_s_email_low ) - 1.

* If the first or last letter is '@' or '.', terminate the subroutine.
  
if p_s_email_low(1) = c_attherateof or p_s_email_low(1) = c_dot.
wf_mail = 1.
exit.
elseif p_s_email_low+lv_length(1) = c_attherateof
or p_s_email_low+lv_length(1) = c_dot.
wf_mail = 1.
exit.
endif.

Check if '@', and '.' exists or not.
  
if ( p_s_email_low ns '@') or ( p_s_email_low ns '.' ).
wf_mail = 1.
exit.
endif.

* Check for white space in email field.

if p_s_email_low(lv_length) ca ' '.
wf_mail = 1.
exit.
endif.

* Look for '@' and separate name, domain for validations.
  
search p_s_email_low for c_attherateof.
if sy-subrc = 0.
lv_search = sy-fdpos.
lv_name = p_s_email_low+0(lv_search).
lv_search = lv_search + 1.
lv_domain = p_s_email_low+lv_search.
endif.

* Check if name or domain is empty.
  
if lv_name is initial or lv_domain is initial.
wf_mail = 1.
exit.
endif.

* Calculate the length of each field.
  
lv_len_name = strlen( lv_name ).
lv_len_domain = strlen( lv_domain ).

* Validate lv_name and lv_domain for second '@'.
  
if lv_name cp '@' or lv_domain cp '@'.
wf_mail = 1.
exit.
endif.

* Validate for min. one dot and max. two dots in domain field.
 
if lv_domain cs '.'.
clear lv_search.
lv_search = sy-fdpos.
if lv_search = 0.
wf_mail = 1.
exit.
else.
lv_search = lv_search + 1.
 if lv_domain+lv_search cs '.'.
if sy-fdpos = 0.
wf_mail = 1.
exit.
else.
lv_search = lv_search + sy-fdpos + 1.
          if lv_domain+lv_search cs '.'.
            wf_mail = 1.
            exit.
          endif.
        endif.
      endif.
    endif.
  else.
    wf_mail = 1.
    exit.
  endif.
endform.                    " VALIDATE_P_MAIL

Note: wf_mail is declared as global field and cleared before subroutine is called. Can act as exporting parameter in case of FM implementation. wf_mail = 0 Valid e-mail ID

Sample Output:

wf_mail = 0 à Valid e-mail ID

wf_mail = 1 à invalid e-mail ID


×