29/09/2010

Using Regex to validate email address


REPORT ZADN_SAMPLE.

*----------------------------------------------------------------------*
* -> CLASS cl_regex_test DEFINITION
*----------------------------------------------------------------------*
CLASS cl_regex_test DEFINITION FINAL.

PUBLIC SECTION.

METHODS : check_email IMPORTING p_email TYPE string.

PRIVATE SECTION.

DATA: o_regex TYPE REF TO cl_abap_regex,
o_matcher TYPE REF TO cl_abap_matcher.

ENDCLASS. "cl_regex_test DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_regex_test IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_regex_test IMPLEMENTATION.

METHOD check_email.

* Create the regular expression:

CREATE OBJECT o_regex
EXPORTING
pattern = '^[a-zA-Z]+(([\''\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+<' &
'(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})>$|^(\w[-._\w]*' &
'\w@\w[-._\w]*\w\.\w{2,3})$'.

* Check to see if the email matches the regular expression:

CREATE OBJECT o_matcher
EXPORTING
regex = o_regex
text = p_email.

IF o_matcher->match( ) NE abap_true.

MESSAGE 'Enter email address valid...' TYPE 'E'.

ELSE.

MESSAGE 'This is a valid email address...' TYPE 'S'.

ENDIF.

ENDMETHOD. "check_email

ENDCLASS. "cl_regex_test IMPLEMENTATION

************************************************************************
************************************************************************
* *
* PROGRAM FLOW LOGIC *
* *
************************************************************************

START-OF-SELECTION.

DATA : ocl_regex_test TYPE REF TO cl_regex_test.

CREATE OBJECT ocl_regex_test.

ocl_regex_test->check_email( 'abapdn.network@mail.com' ).

Nenhum comentário:

Postar um comentário