Here's the one I use. I apologize in advance for any wrapping.
*!* FUNCTION ValidateEmail
LPARAMETERS tcEmail AS Character
ASSERT VARTYPE(tcEMail)=[C] MESSAGE [Parameter type must be character!]
LOCAL llResult AS Boolean && holds correction status so far
LOCAL llFirst AS Boolean && we are at the beginning of a part
LOCAL llAt AS Boolean && a @ is present
LOCAL llPoint AS Boolean && the last char scanned was a point?
LOCAL lnLoop AS Integer
LOCAL llContinue AS Boolean
LOCAL lcChar AS Character
LOCAL lcEMail AS Character
STORE .t. TO llResult, llFirst, llContinue
STORE .f. TO llAt, llPoint
lcEMail=ALLTRIM(tcEmail)
IF VERSION(2)=2
lcOldEscape=SET("Escape")
SET ESCAPE ON
ENDIF
DO WHILE llContinue
IF VARTYPE(lcEMail)<>[C]
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
*!* - rk - 2005-9-23 - count a blank email as invalid. doh!
*!* - rk - 2005-9-30 - got this sideways, sort of. if it's empty, just
return .t.
IF EMPTY(lcEMail)
llContinue=.t.
EXIT
ELSE
llContinue=.t.
ENDIF
IF LEN(ALLTRIM(lcEMail))<6 && minimum length of address is 6
characters (i.e. [EMAIL PROTECTED])
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
*!* test lengths of the parts. the part before the @ can be 64
characters, the part after can be 255
*!* so total length with @ is 320. of course, we are only storing 80
characters so that's another story
IF LEN(ALLTRIM(lcEMail))>320 && exceeds maximum allowable length
llContinue=.f.
EXIT
ELSE && now check the parts
IF LEN(LEFT(lcEMail,AT([EMAIL PROTECTED],lcEMail,1)-1))>64 && this
is the
mailbox name
llContinue=.f.
EXIT
ELSE && now check the domain
IF LEN(SUBSTR(lcEMail,at([EMAIL PROTECTED],lcEMail,1)+1))>255
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
ENDIF
ENDIF
IF OCCURS([EMAIL PROTECTED],lcEMail)<>1 && only one @
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
IF OCCURS([ ],lcEMail)<>0 && don't allow spaces
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
IF OCCURS([..],lcEMail)<>0 && don't allow consecutive periods
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
IF INLIST(LEFT(lcEMail,1),[.],[EMAIL PROTECTED]) OR
INLIST(RIGHT(lcEMail,1),[.],[EMAIL PROTECTED]) && don't allow . or @ as
first or
last character
llContinue=.f.
EXIT
ELSE
llContinue=.t.
ENDIF
IF RAT([.],lcEMail)>AT([EMAIL PROTECTED],lcEMail) && make sure there is
one .
after the @
llContinue=.t.
ELSE
llContinue=.f.
EXIT
ENDIF
*!* now let's check the other rules
FOR lnLoop = 1 TO LEN(lcEMail)
lcChar = SUBSTR(lcEMail,lnLoop,1)
IF lcChar = '.'
IF llFirst OR llPoint
llResult = .F. && can't start with point or repeat it
in sequence
ELSE
llPoint = .T.
ENDIF
ELSE
DO CASE
CASE lcChar = '@'
IF llFirst OR llAt OR llPoint
llContinue= .F. && can't start with @ or repeat it
EXIT
ELSE
STORE .T. TO llFirst, llAt && domain name starts
llPoint = .F.
ENDIF
CASE BITAND(ASC(lcChar),0x80)<>0
llContinue= .F. && non-ASCII is forbidden
EXIT
CASE AT(lcChar,'()<>,;:\"[]')<>0
llContinue= .F. && and so are reserved characters
EXIT
CASE AT(lcChar,['])<>0
llContinue= .F. && and so are reserved characters
EXIT
CASE lcChar<' ' OR lcChar = CHR(0x7f)
llContinue= .F. && and control characters
EXIT
OTHERWISE
STORE .F. TO llFirst, llPoint
ENDCASE
ENDIF
IF NOT llResult && any other rules violation
llContinue=.f.
EXIT
ENDIF
ENDFOR
EXIT
ENDDO && llContinue
llResult = llContinue AND llResult AND llAt AND NOT llPoint AND NOT llFirst
IF VERSION(2)=2
SET ESCAPE &lcOldEscape
ENDIF
RETURN llResult
*!* ENDDEF FUNCTION
Steve Ellenoff wrote:
> Does anyone have some nearly fullproof code to validate an email
> address for proper syntax?
>
--
Richard Kaye
Vice President
Artfact/RFC Systems
Voice: 617.219.1038
Fax: 617.219.1001
For the fastest response time, please send your support
queries to:
Technical Support - [EMAIL PROTECTED]
Australian Support - [EMAIL PROTECTED]
Internet Support - [EMAIL PROTECTED]
All Other Requests - [EMAIL PROTECTED]
---------------------------------------------------------
This message has been checked for viruses before sending.
---------------------------------------------------------
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/profox
OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message: http://leafe.com/archives/byMID/profox/[EMAIL PROTECTED]
** All postings, unless explicitly stated otherwise, are the opinions of the
author, and do not constitute legal or medical advice. This statement is added
to the messages for those lawyers who are too stupid to see the obvious.