I think that that would fail on [EMAIL PROTECTED] though wouldn't it?

-----Original Message-----
From: Paul Johnston [mailto:[EMAIL PROTECTED]] 
Sent: 28 January 2003 13:47
To: [EMAIL PROTECTED]
Subject: [ cf-dev ] Regular Expression for Email and Domain checking -
it works!

Everyone,

For those that can't be bothered to read to the bottom here is the
regular
expression for validating an email address:

^[a-zA-Z0-9!##\$%&''\*\+-/=\?\^_`\{\|}~]+([.][a-zA-Z0-9!##\$%&''\*\+-/=\
?\^_
`\{\|}~]+)*@([a-z0-9]([-]|[a-z0-9])*[a-z0-9][.])+(([a-z0-9]([-]|[a-z0-9]
)*[a
-z0-9])|com|co[.]uk|net|org|com[.]uk)$

Having read the various RFC's around the place about domains and emails,
I
just thought I'd let you know what I'd found out!

Here is the regular expression for validating the part before an @ in an
email address:

[a-zA-Z0-9!#$%&'*+-/=?^_`{|}~]+([.][a-zA-Z0-9!#$%&'*+-/=?^_`{|}~]+)*

Basically, according to RFC 2822 (and I haven't checked what's updated
it
yet, so this may still be incomplete), this is the form that an address
must
take before the @ sign.  It starts with either a digit or a number, or
one
of the other characters in there, and then can include a period, but
must
end in one of those characters (ie not in a period). There are 2 other
forms
of email address, but they are mainly for use internally within a
mailserver
(and are unimportant in terms of functionality of the regex) and it
should
be no problem to ignore them.

My previous email outlined the regular expression for validating a
domain
name (simple form):

([a-z0-9]([-]|[a-z0-9])*[a-z0-9][.])+(([a-z0-9]([-]|[a-z0-9])*[a-z0-9])|
com|
co[.]uk|net|org|com[.]uk)

And you can create a regular expression for validating emails.  Please
note,
that none of this is tested, although it does look okay to me ;).  The
regular expression then for testing if an email is valid is:

[a-zA-Z0-9!#$%&'*+-/=?^_`{|}~]+([.][a-zA-Z0-9!#$%&'*+-/=?^_`{|}~]+)*@([a
-z0-
9]([-]|[a-z0-9])*[a-z0-9][.])+(([a-z0-9]([-]|[a-z0-9])*[a-z0-9])|com|co[
.]uk
|net|org|com[.]uk)

(NOTE: watch the word wrap)

Bearing in mind that CF needs to escape several of the characters in the
regular expression, it becomes this:

[a-zA-Z0-9!##\$%&''\*\+-/=\?\^_`\{\|}~]+([.][a-zA-Z0-9!##\$%&''\*\+-/=\?
\^_`
\{\|}~]+)*@([a-z0-9]([-]|[a-z0-9])*[a-z0-9][.])+(([a-z0-9]([-]|[a-z0-9])
*[a-
z0-9])|com|co[.]uk|net|org|com[.]uk)

And also taking into account that you need to check the whole string, ie
this must be from the start to the end, you get:

^[a-zA-Z0-9!##\$%&''\*\+-/=\?\^_`\{\|}~]+([.][a-zA-Z0-9!##\$%&''\*\+-/=\
?\^_
`\{\|}~]+)*@([a-z0-9]([-]|[a-z0-9])*[a-z0-9][.])+(([a-z0-9]([-]|[a-z0-9]
)*[a
-z0-9])|com|co[.]uk|net|org|com[.]uk)$

You can easily make this into a function or into a custom tag.  In fact,
I'll be nice and write the function for you:

<cfscript>
// returns true or false depending on whether or not it's valid
function isValidEmail(emailstr) {
        regex =
"^[a-zA-Z0-9!##\$%&''\*\+-/=\?\^_`\{\|}~]+([.][a-zA-Z0-9!##\$%&''\*\+-/=
\?\^
_`\{\|}~]+)*@([a-z0-9]([-]|[a-z0-9])*[a-z0-9][.])+(([a-z0-9]([-]|[a-z0-9
])*[
a-z0-9])|com|co[.]uk|net|org|com[.]uk)$";
        if(REFind(regex, emailstr)) {
                return true;
        }
        else
                return false;
}
</cfscript>

(NOTE: watch the word wrap again)

I hope this helps you all sort your email addresses out okay now!

Paul
-------------------------------------------
Paul Johnston
PJ Net Solutions Ltd
http://www.pjnetsolutions.com
[EMAIL PROTECTED]
+44 (0)7866 573013




-- 
** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/

To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For human help, e-mail: [EMAIL PROTECTED]



-- 
** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/

To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For human help, e-mail: [EMAIL PROTECTED]

Reply via email to