midrange.com code scratchpad |
Name:
Email Format Validation
|
Scriptlanguage:
Plain Text
|
Tabwidth:
4
|
Date:
10/26/2009 03:43:30 pm
|
IP:
Logged
|
|
Description:
Base on ^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$
|
Code:
- D UPPER_CASE c const('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
- D LOWER_CASE c const('abcdefghijklmnopqrstuvwxyz')
- D SUCCESSFUL c const('1')
-
- P emlIsValid b Export
- D emlIsValid pi 1n
- D piEml like($EML) const
-
- D @pos s 5i 0
- D dotPos s 5i 0
- D myEmailAdr s 255 varying
- D myRecipient s 255 varying
- D myDomainName s 255 varying
- D myDomainType s 255 varying
- D myValidRecipientChar...
- D c const('ABCDEFGHIJKLMNOPQRSTUVWXYZ012-
- D 3456789._%-')
- D myValidDomainNameChar...
- D c const('ABCDEFGHIJKLMNOPQRSTUVWXYZ012-
- D 3456789.-')
- D myValidDomainTypeChar...
- D c const('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
-
- /free
-
- // check for format of email base on this regular expression
- //^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$'
- //http://systeminetwork.com/article/validate-e-mail-address-regular-expression
-
- exsr parseEmail;
-
- // make sure the recipient portion is correct
- if %len(myRecipient) = 0 or
- %check(myValidRecipientChar :myRecipient) > 0;
- return not SUCCESSFUL;
- endif;
-
- // make sure the domain portion is correct
- if %len(myDomainName) = 0 or
- %check(myValidDomainNameChar :myDomainName) > 0;
- return not SUCCESSFUL;
- endif;
-
- // make sure the domain type portion is correct
- if not (%len(myDomainType) >=2 and
- %len(myDomainType) <=4) or
- %check(myValidDomainTypeChar :myDomainType) > 0;
- return not SUCCESSFUL;
- endif;
-
- return SUCCESSFUL;
-
-
- //*******************************************************
- // parse the email into recipient/domain/dimainType
- //*******************************************************
- begsr parseEmail;
-
- myEmailAdr = %trim(piEml);
- myEmailAdr = %xlate(LOWER_CASE :UPPER_CASE :myEmailAdr);
-
- // parse out the recipient
- @pos = %scan('@' :myEmailAdr);
- if @pos > 1;
- myRecipient = %subst(myEmailAdr :1 :@pos-1);
- endif;
-
- // parse out the Domain
- if %len(myRecipient) > 0;
- for dotPos = %len(myEmailAdr) downto @pos+1;
- if %subst(myEmailAdr :dotPos :1) = '.';
- leave;
- endif;
- endfor;
-
- if dotPos > @pos+1;
- myDomainName = %subst(myEmailAdr :@pos+1 :dotPos-@pos-1);
- endif;
- endif;
-
- // parse out the Domain type
- if %len(myDomainName) > 0;
- if %len(myEmailAdr) > dotPos;
- myDomainType = %subst(myEmailAdr :dotPos+1);
- endif;
- endif;
-
- endsr;
-
- /end-free
-
- p emlIsValid e
|
|
|