05-16-2011 3:52 PM
Dear All,
Requirement:
validate the email ID entered & throw error message, if it is invalid.
DATA c_mailpattern TYPE c LENGTH 60 VALUE
'[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4} '.
** If @ is present, more than once. Error out
find ALL OCCURRENCES OF '@' in P_email
MATCH COUNT v_count.
if v_count > 1.
v_badpattern = 1.
endif.
** If , is present, once, Error out
find ALL OCCURRENCES OF ',' in P_Email
MATCH COUNT v_count.
if v_count > 0.
v_badpattern = v_badpattern + 1.
endif.
FIND REGEX c_mailpattern IN P_Email IGNORING CASE .
IF sy-subrc <> 0 OR v_badpattern > 0.
Write:/ p_EMAIL, 'has invalid Email format'.
ENDIF.
though this works fine, tester needs me to catch, if domain name has "app.com.com" as invalid email id.
above regex fails in such case.
I searched & found
{messageID=3706355}
messageID=1657369}{
https://wiki.sdn.sap.com/wiki/display/Snippets/E-MAIL+Validation
doesn't help.
I found this regex in a perl program.
[a-z0-9!#$%&'{size:14}*+{size:14}/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Can I get help to modify this into ABAP String?
1) I can't bypass the boldened text using Escape characters like #* or '' Can some one help me assign this regex-string into a string variable?
2) This regex is longer than allowed length for a literal.
It can be split into 2 strings, then concatenated & checked.
Edited by: Mallikarjuna J on May 16, 2011 8:23 PM
Edited by: Mallikarjuna J on May 16, 2011 8:26 PM
05-16-2011 4:51 PM
Hi,
You can find many examples for regex email validation( please search for pattern cl_abap_regex cl_abap_matcher email in SCN . Other than that you can also use function SX_INTERNET_ADDRESS_TO_NORMAL).
TYPE-POOLS:sx.
DATA:ls_input TYPE sx_address.
DATA:lv_string TYPE string.
DATA:lv_domain TYPE sx_addr.
PARAMETERS:p_mail TYPE sx_address-address.
ls_input-type = 'INT'.
ls_input-address = p_mail.
CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL'
EXPORTING
address_unstruct = ls_input
complete_address = 'X'
IMPORTING
domain = lv_domain
EXCEPTIONS
error_address_type = 1
error_address = 2
error_group_address = 3
OTHERS = 4.
IF sy-subrc ne 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO lv_string.
MESSAGE i000(zt) WITH lv_string .
else.
if lv_domain = 'XYZ.COM'.
MESSAGE i000(zt) WITH 'Invalid domain' .
endif.
ENDIF.
05-16-2011 4:36 PM
check this sample program, it works for me
PARAMETERS p_valor TYPE c LENGTH 30 LOWER CASE.
*PARAMETERS p_regex TYPE c LENGTH 500 visible length 500.
DATA: l_pat(500) type c.
DATA matcher TYPE REF TO cl_abap_matcher.
l_pat = '^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$'.
matcher = cl_abap_matcher=>create( pattern = l_pat"p_regex
ignore_case = 'X'
text = p_valor ).
IF matcher->match( ) IS INITIAL.
MESSAGE 'Wrong Format' TYPE 'I'.
ELSE.
MESSAGE 'Format OK' TYPE 'I'.
ENDIF.
05-16-2011 4:39 PM
this might just work for you:
DATA: str1 TYPE string,
email TYPE string,
first_part TYPE string,
second_part TYPE string,
str2 TYPE string.
str1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890._%-@'.
str2 = 'APP.COM.COM'.
email = 'FIRST_NAME.LAST_NAME at your.domain'.
SPLIT email AT '@' INTO first_part second_part.
IF sy-subrc <> 0.
WRITE:/ email, 'has invalid Email format'.
ELSE.
IF first_part CO str1.
IF second_part CS str2.
WRITE:/ email, 'has invalid Email format'.
ELSE.
WRITE:/ email, 'has VALID Email format'.
ENDIF.
ELSE.
WRITE:/ email, 'has invalid Email format'.
ENDIF.
ENDIF.
05-16-2011 4:51 PM
Hi,
You can find many examples for regex email validation( please search for pattern cl_abap_regex cl_abap_matcher email in SCN . Other than that you can also use function SX_INTERNET_ADDRESS_TO_NORMAL).
TYPE-POOLS:sx.
DATA:ls_input TYPE sx_address.
DATA:lv_string TYPE string.
DATA:lv_domain TYPE sx_addr.
PARAMETERS:p_mail TYPE sx_address-address.
ls_input-type = 'INT'.
ls_input-address = p_mail.
CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL'
EXPORTING
address_unstruct = ls_input
complete_address = 'X'
IMPORTING
domain = lv_domain
EXCEPTIONS
error_address_type = 1
error_address = 2
error_group_address = 3
OTHERS = 4.
IF sy-subrc ne 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO lv_string.
MESSAGE i000(zt) WITH lv_string .
else.
if lv_domain = 'XYZ.COM'.
MESSAGE i000(zt) WITH 'Invalid domain' .
endif.
ENDIF.
05-16-2011 7:09 PM
Use Webservice to validate the email as shown below:
* Local data declaration
DATA: lo_http_client TYPE REF TO if_http_client,
l_string TYPE string,
l_result TYPE string,
l_str TYPE string,
lt_result_tab TYPE TABLE OF string.
* Clear local variables
CLEAR: l_string,
l_result,
l_str.
REFRESH: lt_result_tab.
CONCATENATE 'http://www.webservicex.net/ValidateEmail.asmx/IsValidEmail?Email='
p_email INTO l_string.
* Create the HTTP Client
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = l_string
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
* Try to send data
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.
* Try to receive data
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
* Get result
l_result = lo_http_client->response->get_cdata( ).
SPLIT l_result AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_result_tab .
* Check for validity
READ TABLE lt_result_tab INTO l_str INDEX 2.
IF sy-subrc = 0.
IF l_str+44(1) <> 't'. "Check if True/Valid
* Raise message, Invalid Email Address
MESSAGE e208(00) WITH text-e02.
STOP.
ENDIF.
ELSE.
* Raise message, Invalid Email Address
MESSAGE e208(00) WITH text-e02.
STOP.
ENDIF.
05-17-2011 9:51 AM
Thanks Sebastian, Pratik & Keshav for the replies.
SX_INTERNET_ADDRESS_TO_NORMAL doesn't validate a wrong email ID. It only splits the internet address into mail & domain.
Prathik,
just .com.com is not the point, Bad input could be .net.ent or .net.com or so....
Amol, Thanks, but I keep receiving Error, not found in the 41 line response I get
I think we need to check not line 2 but line 28.
Taking cue from Prathik, I'm planning to put this
*** ls_inputmail-mail is the email-id entered by user.
************ Check for Valid Regular Expression
***** DOT(.) is allowed more than once,
***** @ is allowed only once,
***** , is not allowed.
** If @ is present, more than once. Error out
find ALL OCCURRENCES OF '@' in ls_input_mail-mail
MATCH COUNT v_count.
if v_count > 1.
v_badpattern = 1.
endif.
** If , is present, once, Error out
find ALL OCCURRENCES OF ',' in ls_input_mail-mail
MATCH COUNT v_count.
if v_count > 0.
v_badpattern = v_badpattern + 1.
endif.
** Find if domain part i.e., after @ has errors.
SPLIT ls_input_mail-mail at '@' into v_mailpart v_domain.
* there's a dot in the domain.
if v_domain Co '.' .
* last 2 char can only be country name, not anything else.
SPLIT v_domain at '@' into v_domain1 v_domain2.
* v_domain2 can only be a country name, else error out
select single landx from t005 into v_country
where landx = v_domain2.
if sy-subrc <> 0.
v_badpattern = v_badpattern + 1.
endif.
ENDIF.
FIND REGEX c_mailpattern IN ls_input_mail-mail IGNORING CASE .
IF sy-subrc <> 0 OR v_badpattern > 0.
Write:/ ls_inputmail-mail, 'has invalid email format'.
ENDIF.
However, I was wondering, if there was a way to use escapae characters & make the beow string as a valid regex variable to check email id.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Nevertheless, Thanks Friends for all your inputs.
Edited by: Mallikarjuna J on May 17, 2011 2:23 PM