469,353 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Checking for a valid email address

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

Jul 20 '05 #1
11 2192
In article <40***************@btinternet.com>, ph******@btinternet.com
enlightened us with...
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: so*******@something.com
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)){

--
--
~kaeli~
Once you've seen one shopping center, you've seen a mall.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2
kaeli <ti******@NOSPAM.comcast.net> writes:
function isEmail(strObject)
{
// returns true if: so*******@something.com
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
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] ?

--
Chris Jeris cj****@oinvzer.net Apply (1 6 2 4)(3 7) to domain to reply.
Jul 20 '05 #4
Christopher Jeris <cj****@oinvzer.net> writes:
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
me*****@example.com
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
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.)

--
Chris Jeris cj****@oinvzer.net Apply (1 6 2 4)(3 7) to domain to reply.
Jul 20 '05 #6
In article <is**********@hotpop.com>, lr*@hotpop.com enlightened us
with...
kaeli <ti******@NOSPAM.comcast.net> writes:
function isEmail(strObject)
{
// returns true if: so*******@something.com
var re = /^.+@.*\.com$/i;


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
so*******@mycompany.com.

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

Jul 20 '05 #7
JRS: In article <is**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Tue, 13 Jan 2004 21:31:24 :-
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 lr*@hotpop.com
and <lr*@hotpop.com>
but rightly also a full Lasse Reichstein Nielsen <lr*@hotpop.com>
It rightly rejects Lasse R. Nielsen <lr*@hotpop.com>
and accepts "Lasse R. Nielsen" <lr*@hotpop.com>
It rightly accepts also the possibly-deprecated form
lr*@hotpop.com (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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #8
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" <cj****@oinvzer.net> wrote in message
news:xi*************@hsph.harvard.edu...
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
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.)

--
Chris Jeris cj****@oinvzer.net Apply (1 6 2 4)(3 7) to domain to reply.

Jul 20 '05 #9
Alan P wrote:
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


Ju******@aol.com is a very valid email address. But unless you are
sending it mail from Hi************@aol.com, 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.

--
Randy

Jul 20 '05 #10
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

"Randy Webb" <hi************@aol.com> wrote in message
news:ku********************@comcast.com...
Alan P wrote:
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


Ju******@aol.com is a very valid email address. But unless you are
sending it mail from Hi************@aol.com, 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.

--
Randy

Jul 20 '05 #11
JRS: In article <ku********************@comcast.com>, seen in
news:comp.lang.javascript, Randy Webb <hi************@aol.com> posted at
Thu, 15 Jan 2004 07:42:51 :-
Alan P wrote:
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

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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by unndunn | last post: by
9 posts views Thread by news | last post: by
30 posts views Thread by Michael B Allen | last post: by
99 posts views Thread by Mikhail Teterin | last post: by
7 posts views Thread by pinkfloydhomer | last post: by
2 posts views Thread by JD | last post: by
26 posts views Thread by libsfan01 | last post: by
10 posts views Thread by frakie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.