ABAP RegEx to Validate Email

By | Last Updated on October 10, 2013 | 6,532

Code Snippet to validate the Email ID using the ABAP RegEx utility classes CL_ABAP_REGEX and CL_ABAP_MATCHER.

RegEx to Validate EMail

    METHODS validate_email_id
        iv_email TYPE ad_smtpadr
        value(result) TYPE flag .
ENDCLASS.                    "lcl_mail DEFINITION
  DATA: lo_mail TYPE REF TO lcl_mail.
  CREATE OBJECT lo_mail.
  IF lo_mail->validate_email_id( 'szdfd@' ) IS INITIAL.
    WRITE: 'Invalid Email'.
    Write: 'Valid'.
  METHOD validate_email_id.
* Local Data
    DATA:  regex   TYPE REF TO cl_abap_regex,       " Regex Object
           matcher TYPE REF TO cl_abap_matcher,     " Matcher Object
           match   TYPE c LENGTH 1,                 " Match ?
           mail_to_check(100).                      " Email ID to check
    mail_to_check = iv_email.
    result = abap_true.  "Email is valid
* Instntiate Regex
        pattern     = 'w+(.w+)*@(w+.)+(w{2,4})'
        ignore_case = abap_true.
* Create the Matcher
    matcher = regex->create_matcher( text = mail_to_check ).
* Match not found, invalid
    IF matcher->match( ) IS INITIAL.
      result = abap_false.  "Email not valid
  ENDMETHOD.                    "validate_email_id
ENDCLASS.                    "lcl_mail IMPLEMENTATION

RegEx Explanation

Based on comment from Benno, I thought of explaining the RegEx.
The first part, in front of @, will accept any alphanumeric character or characters with or without (.) dot. This part (\.\w+) validates if there is a dot it must be followed by a character.

Part following the @, allows the flexibility to have any name with following a (.) dot. This part (\w+\.) makes sure that there is at least one domain before the end part. If there are multiple, it would check for (.) dot after each part of the domain.

The last part ensures that the email has at least 2 and maximum 4 characters after the last dot.

More RegEx for Email Validation

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments


  • Benno

    Hi Naimesh,

    I really love your site and inspiring code snippets!
    and using REGEX for validating Email-Adresses is nice too – but I suffer from many websites that do poor email adress checks. For example my Email adresses all look like this:

    first.last@xx.yyyyyy.ch the domain name can have multiple dots ‘.’ !

    it would be nice if you could change your code to provide for that, so that all people copiying your code,
    can copy the good version.
    thank you Naimesh and keep going !

  • steve oldner

    Thank you Naimesh!

    @Benno – i think we are suppose to do some of the work ourselves, not just copy everything Naimesh does. Actually I was going to copy but change logic to email = false until proven true. I have seen on google some more sophisticated regex for testing emails. Maybe the NSA would share some of their code LOL!!!

  • Hello Benno,

    This RegEx validates the email address provided by you. I have added an explanation for the RegEx. Let me know if you have any email which should be valid but points otherwise by this code.

    @Steve – Please share when you get something from google or NSA ;)

    Naimesh Patel

  • jj

    Hi Naimesh,
    I love your site too; it’s very interesting and usefull for my profession.

    But, I can’t use this example:

    name.lastname@gmail.com -> is invalid
    name@gmail.com -> is invalid

    I can’t understand :(

  • Hello jj,

    Both of them are valid for me on a ABAP release 731. What release, you are on?

    Naimesh Patel

  • jj

    Version: SAP ECC 6.0
    component SAP_ABA = 7.01

    too old, I suppose.


  • Matthew

    You shouldn’t use regex to validate e-mail addresses; it is a flawed approach – the RFC for e-mail addresses is very permissive. For example, consider the .museum TLD, which your regex would disallow.
    The best way to validate an e-mail address is to send an e-mail to it.
    See also: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address

  • Hello Matthew,

    Yes, you are right that it would only validate the TLD (Top Level Domain) with 2 to 4 characters – As you pointed out the .something would fail.

    In SAP, the emails would send out of the server by a batch job. If we send the email to validate if the email-id exist or not, it would have to be a two step approach – 1) Send Test mail, 2) Decode the response and send the actual mail letter on. I believe, it would be unnecessary when your main purpose to send a notification to your organization itself as it could be a group instead of the email id.

    Naimesh Patel

  • Matthew

    If you’re sending an e-mail within your own organisation, your best bet is to connect to whatever directory server you use and use an LDAP query to verify if that particular mailbox exists. I actually wrote something to do this the other day:

         BASE_STRING         = 'DC=yourdomain,DC=com'
         SCOPE               = 2
         FILTER_STRING       = |(&(objectclass=*) (mail={ g_email_addr }))|
         ATTRIBUTES          = lt_attributes
         LDAPRC              = ls_ldaprc
         ENTRIES             = lt_entries
         NO_AUTHORIZ         = 1
         CONN_OUTDATE        = 2
         LDAP_FAILURE        = 3
         NOT_ALIVE           = 4
         OTHER_ERROR         = 5
         OTHERS              = 6

    Then examine ls_ldaprc and if there is a row in lt_entries for the mailbox.

    I use this to validate e-mail addresses from within the organisation that a user has input. All other e-mail addresses are run past a simple check: the string contains an @-sign, at least one dot, and has a length of greater than five. The shortest valid e-mail is something like a@g.cn which is six characters long.

  • Hello Matthew,

    Thanks a lot for the code snippet. I would surely give a try and would try to include in my future developments.

    If you agree, I would put the code in the code snippet section.

    Naimesh Patel

  • Matthew

    Hi Naimesh,

    Yes, feel free to add this as a code snippet :)
    Note you need to call LDAP_SYSTEMBIND first to initiate an LDAP connection.

Comments on this Post are now closed. If you have something important to share, you can always contact me.