Search method or RegExp?

B

Ben Amada

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);
}
 
R

RobG

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/c...e+email+address&rnum=6&hl=en#2b1b7799070dc4ec
Both the examples you posted will refuse valid e-mail addresses, the
second will not allow my current e-mail address.
 
B

Ben Amada

RobG said:
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/c...e+email+address&rnum=6&hl=en#2b1b7799070dc4ec

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
 
T

Thomas 'PointedEars' Lahn

RobG said:
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
 
E

Evertjan.

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);
 
T

The Natural Philosopher

Ben said:
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);
}

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.
 
B

Ben Amada

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)) {

Why not use the last one?

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 (e-mail address removed)

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
 
E

Evertjan.

Ben Amada wrote on 30 aug 2007 in comp.lang.javascript:
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 (e-mail address removed)

As I an sure the minimum after the last period is TWO letters, Ben,
(e-mail address removed) should not be alowed..
 
T

The Natural Philosopher

Evertjan. said:
Ben Amada wrote on 30 aug 2007 in comp.lang.javascript:



As I an sure the minimum after the last period is TWO letters, Ben,
(e-mail address removed) 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'

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top