Hi all. I know very little about regular expressions, but wanted to use one
to validate an email address a user would be entering before the form is
submitted. There are many examples out there. Two two functions I found
below both validate an email address correctly, but work differently. The
first one uses the "search" method of a string, and the second uses a RegExp
object. I'm just wondering which method might be most reliable across the
most number of javascript enabled browsers out there ... the string.search()
method or the RegExp() object?
Any insight is appreciated, thanks!
Ben
function checkOne(sEmail) {
if
(sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
!= -1) {
return true;
} else {
return false;
}
}
function checkTwo(sEmail) {
var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
var regex = new RegExp(emailReg);
return regex.test(sEmail);
}
On Aug 30, 2:03 pm, "Ben Amada" <ben.nojunkplease.am...@gmail.com>
wrote:
> Hi all. I know very little about regular expressions, but wanted to use one
> to validate an email address a user would be entering before the form is
> submitted.
Rigorous validation of e-mail address is impossible, the best you can
do client-side is a rough test then send an email to the address to
verify it.
<URL:
http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/35ab975
1882cdafc/2b1b7799070dc4ec?lnk=gst&q=validate+email+address&rnum=6&hl=en#2b1b779
9070dc4ec
Both the examples you posted will refuse valid e-mail addresses, the
second will not allow my current e-mail address.
--
Rob
Ben Amada - 30 Aug 2007 06:45 GMT
> On Aug 30, 2:03 pm, "Ben Amada" <ben.nojunkplease.am...@gmail.com>
> wrote:
[quoted text clipped - 11 lines]
> Both the examples you posted will refuse valid e-mail addresses, the
> second will not allow my current e-mail address.
Hi Rob. Thanks for that link which lead me to the very informative page by
Dr. Stockton. I'm actually not looking for a rigorous test, just a simple
test to execute on top of making sure some input has been entered. I'd
honestly much rather err on the side of letting a questionable email address
slip thru the validation test. I think I have an updated test, based on
what I saw, that will not incorrectly reject a valid email address.
function checkSix(sEmail) {
if (sEmail.search(/(\S+@\S+\.\S+)/) != -1) {
return true;
} else {
return false;
}
}
I ran your AU email address against this function with a 'true' response.
For what it's worth, I also saw a 'true' response with the checkTwo()
function I had in my original post when passing your AU email address into
it.
Thanks again for the reply,
Ben
David Golightly - 30 Aug 2007 07:38 GMT
On Aug 29, 10:45 pm, "Ben Amada" <ben.nojunkplease.am...@gmail.com>
wrote:
> if (sEmail.search(/(\S+@\S+\.\S+)/) != -1) {
Write:
if (/(\S+@\S+\.\S+)/.test(sEmail)) {
instead. Marginally more efficient.
-David
Evertjan. - 30 Aug 2007 09:24 GMT
Ben Amada wrote on 30 aug 2007 in comp.lang.javascript:
> function checkSix(sEmail) {
> if (sEmail.search(/(\S+@\S+\.\S+)/) != -1) {
When testing with regex, use test(),
which returns a boolean value.
if (/(\S+@\S+\.\S+)/.test(sEmail)) {
better:
if (/^\S+@\S+\.\S{2,}$/.test(sEmail)) {
> return true;
> } else {
> return false;
> }
>}
if boolean {
return true;
} else {
return false;
};
is the same as:
if boolean {
return true;
};
return false;
is the same as:
return boolean;
Why not use the last one?
return /^\S+@\S+\.\S\S+$/.test(sEmail);

Signature
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Ben Amada - 30 Aug 2007 16:54 GMT
> When testing with regex, use test(),
> which returns a boolean value.
[quoted text clipped - 8 lines]
>
> return /^\S+@\S+\.\S\S+$/.test(sEmail);
Thanks all ... I looked over all the helpful suggestions, and have it
down to a simple one-liner. Evertjan, the last one you posted right
above didn't seem to like an address such as a@b.c
A boolean return value which the "test" method returns does seem more
intuitive than a check against -1. I'm guessing from the examples
everyone posted that using the "test" or even the "search" methods are
not any less supported by browsers than the RegExp object which I
don't recall anyone including in their examples. At any rate, I'm
going with the following validation test:
function checkEmail(sEmail) {
return (/(\S+@\S+\.\S+)/.test(sEmail));
}
Thanks all,
Ben
Evertjan. - 30 Aug 2007 17:18 GMT
Ben Amada wrote on 30 aug 2007 in comp.lang.javascript:
>> When testing with regex, use test(),
>> which returns a boolean value.
[quoted text clipped - 12 lines]
> down to a simple one-liner. Evertjan, the last one you posted right
> above didn't seem to like an address such as a@b.c
As I an sure the minimum after the last period is TWO letters, Ben,
a@b.c should not be alowed..
> A boolean return value which the "test" method returns does seem more
> intuitive than a check against -1. I'm guessing from the examples
[quoted text clipped - 9 lines]
> Thanks all,
> Ben

Signature
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
The Natural Philosopher - 31 Aug 2007 04:06 GMT
> Ben Amada wrote on 30 aug 2007 in comp.lang.javascript:
>
[quoted text clipped - 4 lines]
> As I an sure the minimum after the last period is TWO letters, Ben,
> a@b.c should not be allowed..
Sshh!. Don't tell anyone..
Lets hope the same regexp is used by all the web/net crawlers, or
there's gonna be a lot of lookups on the main DNS root servers for
domain '.c'
:-)
Thomas 'PointedEars' Lahn - 30 Aug 2007 08:52 GMT
>> Hi all. I know very little about regular expressions, but wanted to use one
>> to validate an email address a user would be entering before the form is
>> submitted.
>
> Rigorous validation of e-mail address is impossible,
Correct, client-side. Server-side you stand a very good chance.
> the best you can do client-side is a rough test
It would not be too difficult to implement the address specification of
RFC2822 in a Regular Expression though. I have done that twice or more
now here, so searching the archives is probably not a bad idea.
> then send an email to the address to verify it.
It has been my observation that is possible server-side to get a reliable
test result in most cases without sending an e-mail, using an automated
SMTP probe, provided the testing server is, or has access to, a properly
configured MX.
But, as you say, only sending an e-mail and having the receiver confirm
it can make sure that e-mails to the mailbox are actually read.
PointedEars

Signature
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
> Hi all. I know very little about regular expressions, but wanted to use one
> to validate an email address a user would be entering before the form is
[quoted text clipped - 23 lines]
> return regex.test(sEmail);
> }
have written more input validation programs than I care to
mention..frankly the best way is to scan letter by letter with a state
machine and a series of nested switch statements.
It may not look as terse as the regexp stuff, but in the end I don't
suppose it uses any more CPU cycles, and has the benefit that you can in
fact draw up a state machine chart, specify the thing completely, and
document every single line of code with what it does..so at least you
have a small hope of being able to debug it later on.