This code helps in validating an email that requires following validation.
1. Format should be X@Y.Z where X, Y, Z can be alphabets, characters, Special characters.
2. X, Y, Z should not be Null.
3. Contains '@' and '.' in that sequence.
4. There should be a minimum of one dot and maximum of two dots after '@'.
5. E-mail address should not contain any white space.
2. X, Y, Z should not be Null.
3. Contains '@' and '.' in that sequence.
4. There should be a minimum of one dot and maximum of two dots after '@'.
5. E-mail address should not contain any white space.
Here is the code,
Let's say i created a subruotine called validate_mail with a single parameter called gv_mail. Comments itself will explain the code.
*&---------------------------------------------------------------------*
*& Form validate_mail
*&---------------------------------------------------------------------*
* Validates Email entered
*----------------------------------------------------------------------*
* -->P_GV_EMAIL text
*----------------------------------------------------------------------*
FORM validate_mail USING P_GV_EMAIL.
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_gv_email ) - 1.
* If the first or last letter is '@' or '.', terminate the subroutine.
if p_gv_email(1) = co_attherateof or p_gv_email(1) = co_dot.
wf_mail = 1.
exit.
elseif p_gv_email+lv_length(1) = co_attherateof
or p_gv_email+lv_length(1) = co_dot.
wf_mail = 1.
exit.
endif.
* Check if '@', and '.' exists or not.
if p_gv_email np '@' or p_gv_email np '.'.
wf_mail = 1.
exit.
endif.
* Check for white space in email field.
if p_gv_email(lv_length) ca ' '.
wf_mail = 1.
exit.
endif.
* Look for '@' and separate name, domain for validations.
search p_gv_email for co_attherateof.
if sy-subrc = 0.
lv_Search = sy-fdpos.
lv_name = p_gv_email+0(lv_Search).
lv_Search = lv_Search + 1.
lv_domain = p_gv_email+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 cp '.'.
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 cp '.'.
if sy-fdpos = 0.
wf_mail = 1.
exit.
else.
lv_Search = lv_search + sy-fdpos + 1.
if lv_domain+lv_Search cp '.'.
wf_mail = 1.
exit.
endif.
endif.
endif.
endif.
else.
wf_mail = 1.
exit.
endif.
ENDFORM. " validate_mail
After calling this subrotuine, we will check if wf_mail is 0 or 1. If wf_mail = 1, we raise an error message.
*& Form validate_mail
*&---------------------------------------------------------------------*
* Validates Email entered
*----------------------------------------------------------------------*
* -->P_GV_EMAIL text
*----------------------------------------------------------------------*
FORM validate_mail USING P_GV_EMAIL.
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_gv_email ) - 1.
* If the first or last letter is '@' or '.', terminate the subroutine.
if p_gv_email(1) = co_attherateof or p_gv_email(1) = co_dot.
wf_mail = 1.
exit.
elseif p_gv_email+lv_length(1) = co_attherateof
or p_gv_email+lv_length(1) = co_dot.
wf_mail = 1.
exit.
endif.
* Check if '@', and '.' exists or not.
if p_gv_email np '@' or p_gv_email np '.'.
wf_mail = 1.
exit.
endif.
* Check for white space in email field.
if p_gv_email(lv_length) ca ' '.
wf_mail = 1.
exit.
endif.
* Look for '@' and separate name, domain for validations.
search p_gv_email for co_attherateof.
if sy-subrc = 0.
lv_Search = sy-fdpos.
lv_name = p_gv_email+0(lv_Search).
lv_Search = lv_Search + 1.
lv_domain = p_gv_email+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 cp '.'.
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 cp '.'.
if sy-fdpos = 0.
wf_mail = 1.
exit.
else.
lv_Search = lv_search + sy-fdpos + 1.
if lv_domain+lv_Search cp '.'.
wf_mail = 1.
exit.
endif.
endif.
endif.
endif.
else.
wf_mail = 1.
exit.
endif.
ENDFORM. " validate_mail
After calling this subrotuine, we will check if wf_mail is 0 or 1. If wf_mail = 1, we raise an error message.