473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 2412
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: unndunn | last post by:
I'm trying to design a regular expression that matches (using preg_match()) when a string is a well-formed Email address. So far I have this: /^+@+\.{2,4}$/i I got that from...
9
by: news | last post by:
There's all kinds of ways to validate an email address to make sure it's well formed and whatnot, but what about checking to see if it's a valid e-mail account? Like how you can use checkdnsrr()...
30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
7
by: pinkfloydhomer | last post by:
When a function is given a pointer, it is tempting to do an assert on the pointer to see if it is not NULL. But even if it is not NULL, it could still be invalid or wrong. What we really want to...
2
by: JD | last post by:
Within asp.net I know it is possible to validate that a user has entered in a correctly formatted email address. However, what I want to be able to do is to verify that the email address entered is...
26
by: libsfan01 | last post by:
Hi all! Can anyone show me how to check and email field on a form for the existence of these two characters. Kind regards Marc
5
by: zafar | last post by:
hi everyone, I have a textbox which contains the e-mail address of user, but I have no Idea how to apply the regular expression in VB.net, what would be the solution for the is e-mail given in text...
10
by: frakie | last post by:
Hi 'body, is there a method to check if a pointer is pointing a freed memory location?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.