Tutorial Name

Validate email address using regular expression in SAP ABAP

Tutorial Description

Validate email address using a common regular expression in SAP ABAP programming

Tutorial Area Advanced ABAP
Prerequisites ABAP
Learning Level Intermediate
Estimated Time to learn 15
Save to Learning List Save to your Learning List
+ -

Step1:Validate email address using regular expression in SAP ABAP Normal Top^

SAP ABAP- Validate email address using a regular expression, by using this we can not validate the existance of email address but we can validate uviversal email format.

Resourse used : SAP class CL_ABAP_REGEX and CL_ABAP_MATCHER .

Regular expression for email validation

'^[_a-zA-Z0-9-] (\.[_a-zA-Z0-9-] )*@[a-zA-Z0-9-] (\.[a-zA-Z0-9-] )*(\.[a-zA-Z]{2,4})$' 
Declare classes
DATA:GO_REGEX    TYPE REF TO CL_ABAP_REGEX,
     GO_MATCHER  TYPE REF TO CL_ABAP_MATCHER.
Add regular expression.
DATA: L_PAT(500) TYPE C.

L_PAT = '^[_a-zA-Z0-9-] (\.[_a-zA-Z0-9-] )*@[a-zA-Z0-9-] (\.[a-zA-Z0-9-] )*(\.[a-zA-Z]{2,4})$' .
Create object for regex class
CREATE OBJECT GO_REGEX
  EXPORTING
    PATTERN     = L_PAT
    IGNORE_CASE = ABAP_TRUE.
Create macher and check match
GO_MATCHER = GO_REGEX->CREATE_MATCHER( TEXT =  'Emailid' ). "email to validate
IF GO_MATCHER->MATCH( ) IS INITIAL.
  WRITE:/ 'Email is not valid'.
ELSE.
  WRITE :/ 'Email is valid'.
ENDIF.

Example program for validating email

REPORT ZSAPN_VALIDATE_EMAIL.
PARAMETERS P_EMAIL TYPE STRING.
DATA:GO_REGEX    TYPE REF TO CL_ABAP_REGEX,
     GO_MATCHER  TYPE REF TO CL_ABAP_MATCHER.

DATA: L_PAT(500) TYPE C.

L_PAT = '^[_a-zA-Z0-9-] (\.[_a-zA-Z0-9-] )*@[a-zA-Z0-9-] (\.[a-zA-Z0-9-] )*(\.[a-zA-Z]{2,4})$' .

CREATE OBJECT GO_REGEX
  EXPORTING
    PATTERN     = L_PAT
    IGNORE_CASE = ABAP_TRUE.
TRANSLATE P_EMAIL TO LOWER CASE.
GO_MATCHER = GO_REGEX->CREATE_MATCHER( TEXT =  P_EMAIL )."lt_node_op4 ).
IF GO_MATCHER->MATCH( ) IS INITIAL.
  WRITE:/ 'Email is not valid'.
ELSE.
  WRITE :/ 'Email is valid'.
ENDIF.

Author | Last Updated| | 2 Comments | F Share

Tutorial Comments

Total Comments: Add your Comment
03 Nov 2014

I have been testing the regular expression and it seems that it doesn't work... Maybe with --> L_PAT = '\w (\.\w )*@(\w \.) (\w{2,4})'. Regards, Rafa

26 Jul 2016

fsfsfsfssfssgggggggggggggggggggggggggggggggggggggggggggggg

Leave a Comment

Enter Comment Text