Two ways to Check if email is valid in ABAP : Function Vs Regex

Check if email is valid is a common business requirement for input email address field.

Email is structural list
[mail]@[Domain].[Extension]

Email Regular Expression (Regex)

General Email Regex

Up to RFC 5322 Official Standard, which defines the Standard of email address, the Regex must be

(?:[a-z0-9!#$%&’*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*|”(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|[x01-x09x0bx0cx0e-x7f])*”)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|[x01-x09x0bx0cx0e-x7f])+)])

Regex in ABAP

The most used Regular Expression in ABAP for email address is:

‘^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[azA-Z]{2,9})

Regex in Javascript

For Javascript, the regular expression is, hopefully, shorter 🙂

^[-a-z0-9~!$%^&*_=+}{‘?]+(.[-a-z0-9~!$%^&*_=+}{‘?]+)*@([a-z0-9_][-a-z0-9_]*(.[-a-z0-9_]+)*.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(:[0-9]{1,5})?$/i

Email Regex for other languages

In this link, find the long list of Email Regular Expression for others common languages:
Python, Swift, PHP, Perl / Ruby, Ruby , .NET.

Check if email is Valid

Check if email is valid for input Field SAP

For the input field, make sure you define the email address field based on the data element AD_SMTPADR.
SAP will handle the validation of the email automatically.

The Domain of data element AD_SMTPADR is name also AD_SMTPADR.

AD_SMTPADR

The Domain AD_SMTPADR has 2 Conversion routines:
CONVERSION_EXIT_SXIDN_INPUT
CONVERSION_EXIT_SXIDN_OUTPUT

Check if email is valid in ABAP with Function

In ABAP, the validity of the email address can be check with the standard function
SX_INTERNET_ADDRESS_TO_NORMAL.
PS: This function is called in the conversion routine of AD_SMTPADR (CONVERSION_EXIT_SXIDN_INPUT).

The type of Address is ‘INT’ for Address email.

  TYPES: sx_addr_type  TYPE sx_addrtyp, "R/3 Addresstype
         sx_addr       TYPE so_rec_ext . "Address in plain string

  TYPES: BEGIN OF sx_address,           "SAPconnect general addr
         type        TYPE sx_addr_type,
         address     TYPE sx_addr,
       END OF sx_address.

  CONSTANTS: cx_t_int    TYPE sx_addr_type VALUE 'INT'.  "SMTP address

  DATA: ls_addr_unst      TYPE sx_address.
  DATA: ls_addr           TYPE sx_address.
  DATA: lv_address_normal TYPE sx_address.
  DATA: lv_local          TYPE sx_addr.
  DATA: lv_domain         TYPE sx_addr.
  DATA: lv_comment        TYPE sx_addr.

  ls_addr_unst-type    = cx_t_int.
  ls_addr_unst-address = p_email. " P_email : input

  CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL'
    EXPORTING
      address_unstruct     = ls_addr_unst
    IMPORTING
      address_normal       = lv_address_normal
      local                = lv_local
      domain               = lv_domain
      comment              = lv_comment
      addr_normal_no_upper = ls_addr
    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.
  ENDIF.

Check if email is valid in ABAP with Regex

The Regular expression for email to be used in ABAP is
^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[azA-Z]{2,9})$

The Class CL_ABAP_MATCHER provides methods to create regular expression and do match check.

  DATA: lv_pattern TYPE string.
  DATA: lv_success TYPE char1. "abap_pool. " ABAP_FALSE/TRUE
  DATA: lr_matcher TYPE REF TO cl_abap_matcher.

  lv_pattern = '^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[azA-Z]{2,9})
.
  lr_matcher = cl_abap_matcher=>create(
                      pattern = lv_pattern
                      text = '[email protected]' ) .
  TRY.
      lr_matcher = cl_abap_matcher=>create(
            pattern       = lv_pattern
            text          = p_email " P_EMAIL: input
            ignore_case   = abap_false
            simple_regex  = abap_false
            no_submatches = abap_false ).

      CALL METHOD lr_matcher->match
        RECEIVING
          success = lv_success.

      IF lv_success = abap_true.
        MESSAGE 'Email is valid' TYPE 'I'.
      ELSE.
        MESSAGE 'Email is not valid' TYPE 'W'.
      ENDIF.
    CATCH cx_sy_regex.
      MESSAGE 'Error occurs' TYPE 'E'.
    CATCH cx_sy_matcher .
      MESSAGE 'Error occurs' TYPE 'E'.
  ENDTRY.