Validate email address using regular expression in SAP ABAP详解编程语言

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

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.

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/20140.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论