473,804 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2496
In article <40************ ***@btinternet. com>, ph******@btinte rnet.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(strObje ct)
{
/* 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(strObj ect.value)) return false;
else return true;
}

function isEmail(strObje ct)
{
// returns true if: so*******@somet hing.com
var re = /^.+@.*\.com$/i;
if (!strObject || isBlank(strObje ct)) return false;
if (!re.test(strOb ject.value)) return false;
else return true;
}

if (! isEmail(documen t.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******@NOSPA M.comcast.net> writes:
function isEmail(strObje ct)
{
// returns true if: so*******@somet hing.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/rasterTriangleD OM.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/rasterTriangleD OM.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**********@h otpop.com>, lr*@hotpop.com enlightened us
with...
kaeli <ti******@NOSPA M.comcast.net> writes:
function isEmail(strObje ct)
{
// returns true if: so*******@somet hing.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*******@mycom pany.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**********@h otpop.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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

"Christophe r Jeris" <cj****@oinvzer .net> wrote in message
news:xi******** *****@hsph.harv ard.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.co m 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5589
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 reguar-expressions.info. But PHP keeps complaining of "Unknown modifier 'Z'". For the life of me I can't figure out how 'Z' is a modifier. Can anyone help? Thanks much.
9
4971
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() to check to see if a URL is valid. I know finger used to be used at one time, no? But server block finger requests, and I'm not sure many e-mail accounts out there are even fingerable type accounts anyway. Thanks for any suggestions!
30
2260
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; return NULL; }
99
5220
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
7
1561
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 know is that it points to memory that we have access to. How about doing this: void f(int* p) { assert(*((char*)p) == *((char*)p); }
2
1376
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 actually going to work. As in that it will receive email. I have been looking at one way to do this, and that is to see if the domain part of the email address is a valid domain, but I was wondering if there was a better way of checking to see...
26
2829
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
4720
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 box have proper format (e.g nameid@domain.com), can anyone tell me the solution for this, the reply would be appriciated.... thanx
10
8340
by: frakie | last post by:
Hi 'body, is there a method to check if a pointer is pointing a freed memory location?
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.