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.
Hi! Try with this FM SX_INTERNET_ADDRESS_TO_NORMAL
ReplyDeleteAND NOW CORRECT CODE:)
ReplyDeleteCONSTANTS: co_attherateof TYPE c VALUE '@', co_dot TYPE c VALUE '.'.
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,
* i_email TYPE string,
r_valid TYPE i.
* clear indicator for mail validation which is used to raise error msg
CLEAR: r_valid,
lv_search,
lv_length,
lv_len_name,
lv_len_domain,
lv_name,
lv_domain,
* i_email,
r_valid.
PARAMETERS: i_email TYPE string DEFAULT 'zzz@zzz.zz'.
* Calculate the length of the email field.
lv_length = STRLEN( i_email ) - 1.
* If the first or last letter is '@' or '.', terminate the subroutine.
IF i_email(1) = co_attherateof OR i_email(1) = co_dot.
r_valid = 1.
* EXIT.
ELSEIF i_email+lv_length(1) = co_attherateof
OR i_email+lv_length(1) = co_dot.
r_valid = 1.
* EXIT.
ENDIF.
* Check if '@', and '.' exists or not.
IF i_email NP '*@*' OR i_email NP '*.*'.
r_valid = 1.
* EXIT.
ENDIF.
* Check for white space in email field.
IF i_email(lv_length) CA ' '.
r_valid = 1.
* EXIT.
ENDIF.
* Look for '@' and separate name, domain for validations.
SEARCH i_email FOR co_attherateof.
IF sy-subrc = 0.
lv_search = sy-fdpos.
lv_name = i_email+0(lv_search).
lv_search = lv_search + 1.
lv_domain = i_email+lv_search.
ENDIF.
* Check if name or domain is empty.
IF lv_name IS INITIAL OR lv_domain IS INITIAL.
r_valid = 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 '*@*'.
r_valid = 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.
r_valid = 1.
* EXIT.
ELSE.
lv_search = lv_search + 1.
IF lv_domain+lv_search CP '*.*'.
IF sy-fdpos = 0.
r_valid = 1.
* EXIT.
ELSE.
lv_search = lv_search + sy-fdpos + 1.
IF lv_domain+lv_search CP '*.*'.
r_valid = 1.
* EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ELSE.
r_valid = 1.
* EXIT.
ENDIF.
IF r_valid = 0.
IF lv_name CA '~`#$%^&*()[]{}:;"''''?/<>,-+=!№'.
r_valid = 1.
ELSE.
r_valid = 0.
ENDIF.
ENDIF.
write r_valid.
Nice solution thanks for psoting. One suggestion I would make is to change the following code from:
ReplyDelete"if p_gv_email np '@' or p_gv_email np '.'."
To:
"if p_gv_email na '@' or p_gv_email na '.'."