Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Validate Email by regular Expression... Need Help

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

Former Member
Not what you were looking for? View more on this topic or Ask a question