Checking for a valid email address

P

Phil Amey

In a web based form I am able to make sure that there is text in an input field but I want to force the user into inputting a valid email
address, one that has @ in the address

How can I modify this JavaScript below to enable this ?

if (document.form1.EMAIL.value == ""){
alert("Please complete the E-Mail: field")
document.form1.EMAIL.focus()
validFlag = false
return validFlag
}

Kindest Regards - Philip Amey
 
K

kaeli

In a web based form I am able to make sure that there is text in an input field but I want to force the user into inputting a valid email
address, one that has @ in the address

How can I modify this JavaScript below to enable this ?

if (document.form1.EMAIL.value == ""){
alert("Please complete the E-Mail: field")
document.form1.EMAIL.focus()
validFlag = false
return validFlag
}

Kindest Regards - Philip Amey

(these are my functions from my validation file, so you can put this all
in one if you like, I just copied/pasted them here)

function isBlank(strObject)
{
/* Returns true if the field is blank, false if not.
You must pass in an input (text) object (not the value) */
var re = /\S+/;

if (!strObject) return true;
if (re.test(strObject.value)) return false;
else return true;
}

function isEmail(strObject)
{
// returns true if: (e-mail address removed)
var re = /^.+@.*\.com$/i;
if (!strObject || isBlank(strObject)) return false;
if (!re.test(strObject.value)) return false;
else return true;
}

if (! isEmail(document.form1.EMAIL)){

--
 
L

Lasse Reichstein Nielsen

kaeli said:
function isEmail(strObject)
{
// returns true if: (e-mail address removed)
var re = /^.+@.*\.com$/i;

As a non-.com-user, I would recommend just
var re = /.+@.+\..+/;
It accepts anything with a @ and a . in that order and with something
around it. Most attempts at being more precise will usually rule out
some perfectly good e-mail address.

/L
 
C

Christopher Jeris

Lasse Reichstein Nielsen said:
As a non-.com-user, I would recommend just
var re = /.+@.+\..+/;
It accepts anything with a @ and a . in that order and with something
around it. Most attempts at being more precise will usually rule out
some perfectly good e-mail address.

Danny Goodman recommends

/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/

(JavaScript & DHTML Cookbook section 8.2). Is there a known problem
with this expression?

(briefly, word (.word)* @ (word.)+ gTLD, where 'word' := [\w-]+ and
'gTLD' := [a-zA-Z]{2,7}.)

I suppose it might be better to write [[:alnum:]_-] for [\w-]
and [[:alpha:]] for [a-zA-Z] ?
 
L

Lasse Reichstein Nielsen

Christopher Jeris said:
Danny Goodman recommends

/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
(JavaScript & DHTML Cookbook section 8.2). Is there a known problem
with this expression?

It doesn't allow
(e-mail address removed)
or
me@[127.0.0.1]
which are both valid e-mail addresses.

A validator of e-mail addresses should first and foremost accept all
valid addresses, otherwise it will interfere with proper use. So, if
it errs, it should err on the side of safety and accept unless there
is a reason to reject. It should at least accept all valid adresses
according to RFC 2822 section 3.4 (and that's a lot).
---quote---
An addr-spec is a specific Internet identifier that contains a
locally interpreted string followed by the at-sign character ("@",
ASCII value 64) followed by an Internet domain.
-----------

Maybe I shouldn't test for the period, just the presence of @.
I suppose it might be better to write [[:alnum:]_-] for [\w-]
and [[:alpha:]] for [a-zA-Z] ?

Those are Perl's long class names. It won't work in Javascript.

(I wish there was an escape for "\w except \d" :)
/L
 
K

kaeli

[email protected] enlightened us said:
As a non-.com-user, I would recommend just

Sorry about that.
I forget about all the variations of the WWW sometimes (you guys keep me
well-rounded LOL).
This was taken from an intranet snippet where the only valid e-mail was
(e-mail address removed).

I like the other one you posted for WWW use.
var re = /.+@.+\..+/;

Thanks!

--
--
~kaeli~
All I ask is the chance to prove that money cannot make me
happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
C

Christopher Jeris

Lasse Reichstein Nielsen said:
It should at least accept all valid adresses
according to RFC 2822 section 3.4 (and that's a lot).
---quote---
An addr-spec is a specific Internet identifier that contains a
locally interpreted string followed by the at-sign character ("@",
ASCII value 64) followed by an Internet domain.
-----------

Well, the text following your quote indicates that there are a _few_
restrictions (for instance, I don't believe an unquoted left bracket
is legal in an 'addr-spec'), but you're absolutely correct, Goodman's
expression is too restrictive to match all addresses. Thank you.
I suppose it might be better to write [[:alnum:]_-] for [\w-]
and [[:alpha:]] for [a-zA-Z] ?
Those are Perl's long class names. It won't work in Javascript.

Doh! Stupid Chris reads the ColdFusion manual, sees that CF
implemented (some of the) long class names from POSIX, and infers
without any justification whatsoever that they work in JavaScript
too. Sorry! (It made sense at the time, honest.)
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
As a non-.com-user, I would recommend just
var re = /.+@.+\..+/;
It accepts anything with a @ and a . in that order and with something
around it. Most attempts at being more precise will usually rule out
some perfectly good e-mail address.


There is the question of purpose.

If the wish is to accept anything that includes what might be an E-mail
address, one may need just that. It does, however, accept itself, which
seems generous.

I doubt whether anyone would intentionally provide a "normal" address
with anything but letters (plural) in the final field, so such a test
for that field being alpha 2+ should be safe enough, and would catch a
few gross blunders.


It is not the test used by my mailer Turnpike, though.

Turnpike will accept a mere (e-mail address removed)
and <[email protected]>
but rightly also a full Lasse Reichstein Nielsen <[email protected]>
It rightly rejects Lasse R. Nielsen <[email protected]>
and accepts "Lasse R. Nielsen" <[email protected]>
It rightly accepts also the possibly-deprecated form
(e-mail address removed) (Denmark)
It rejects, in those, unmatched punctuation.


Where a form requires that an E-mail address be provided, the designer
should consider whether a fuller form should be allowed; and, if so,
that fuller form should be validated to Internet standards.

If collecting name & address * date of birth & e-mail address, the extra
may not be needed. But if the E-address given is to be used, then one
should IMHO be allowed to give it in a full form.
 
A

Alan P

Just a minor note:

From a server-side point of view, you can write a Perl program that pings
the domain to check it exists; stopping people from writing a@a

Christopher Jeris said:
Lasse Reichstein Nielsen said:
It should at least accept all valid adresses
according to RFC 2822 section 3.4 (and that's a lot).
---quote---
An addr-spec is a specific Internet identifier that contains a
locally interpreted string followed by the at-sign character ("@",
ASCII value 64) followed by an Internet domain.
-----------

Well, the text following your quote indicates that there are a _few_
restrictions (for instance, I don't believe an unquoted left bracket
is legal in an 'addr-spec'), but you're absolutely correct, Goodman's
expression is too restrictive to match all addresses. Thank you.
I suppose it might be better to write [[:alnum:]_-] for [\w-]
and [[:alpha:]] for [a-zA-Z] ?
Those are Perl's long class names. It won't work in Javascript.

Doh! Stupid Chris reads the ColdFusion manual, sees that CF
implemented (some of the) long class names from POSIX, and infers
without any justification whatsoever that they work in JavaScript
too. Sorry! (It made sense at the time, honest.)
 
R

Randy Webb

Alan said:
Just a minor note:

From a server-side point of view, you can write a Perl program that pings
the domain to check it exists; stopping people from writing a@a

(e-mail address removed) is a very valid email address. But unless you are
sending it mail from (e-mail address removed), then it won't get delivered.

While you have a valid point about a@a, the idea of trying to ensure an
email address is "valid" is two-fold:

1) Is it a valid format
2) Does the email address actually exist?

format is an overly discussed subject and existence has been debated
before. Neither of which is very easy to accomplish.

The only way I know of to ensure and email address actually exists is to
try to email it and see if it goes through, which still doesn't confirm
its ability to recieve email.
 
A

Alan P

Hmmmm thats a good idea

Like on many web-forums, where you have to activate the e-mail address
before being granted access

Don't think that solution is applicable here though
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in

In that sense, my merlyn.dcu exists only about 2% of the time. However,
mail can be sent to it at any time, and, if the conditions are
propitious, it will be fully received.

OTOH, my www.merlyn.dcu exists permanently, might return a ping, and
never receives mail.

While you have a valid point about a@a, the idea of trying to ensure an
email address is "valid" is two-fold:

1) Is it a valid format
2) Does the email address actually exist?

format is an overly discussed subject and existence has been debated
before. Neither of which is very easy to accomplish.

The first is certainly possible; just read all the RFCs and implement
correct checks. The second is certainly impossible in at least some
cases at some times.
The only way I know of to ensure and email address actually exists is to
try to email it and see if it goes through, which still doesn't confirm
its ability to recieve email.

The only ways to know whether an address receives mail are to mail it
and get a human reply, and to be at the receiving end.

At this moment, two addresses here receive ... but now they do not, and
two others do -- and the machine was not connected to the Net during
this sentence.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top