By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

checking email address for @ and . characters

P: n/a
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

Jun 24 '06 #1
Share this Question
Share on Google+
26 Replies


P: n/a
libsfan01 wrote:
Can anyone show me how to check and email field on a form for the
existence of these two characters.


<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
// validates the format so*******@otherchars.yetotherchars
function cM() {
/.+@.+\..+/.test(document.mF.email.value) ?
alert('OK') : alert('not OK');
}
</script>
</head>
<body>
<form name="mF" action="script.php">
<input type="text" name="email">
<input type="button" value="Check!" onClick="cM()">
</form>
</body>
</html>

(thanks Dr John Stockton,
http://www.merlyn.demon.co.uk/js-valid.htm#VEmA)

--
Bart

Jun 24 '06 #2

P: n/a
Rik
Bart Van der Donck wrote:
function cM() {
/.+@.+\..+/.test(document.mF.email.value) ?
alert('OK') : alert('not OK');
}

Or
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i

Allthough I'm not sure which characters are actually allowed in an
emailadress.

With the above code .@...%0dBcc:sp**@example.net;mo**@example.net seems a
totally valid emailadress..
Then again, I don't know where the validation is for, it could be
sufficient.

Grtz,
--
Rik Wasmus
Jun 24 '06 #3

P: n/a

Rik wrote:
Bart Van der Donck wrote:
function cM() {
/.+@.+\..+/.test(document.mF.email.value) ?
alert('OK') : alert('not OK');
}

Or
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i


It is possible to have a one chracter domain name now or soon will be,
and a 5 character domain name too.

eg: .travel

Jun 24 '06 #4

P: n/a
Rik
Noggon wrote:
Rik wrote:
Bart Van der Donck wrote:
function cM() {
/.+@.+\..+/.test(document.mF.email.value) ?
alert('OK') : alert('not OK');
}

Or
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i


It is possible to have a one chracter domain name now or soon will be,
and a 5 character domain name too.


check,
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i

Now, who can shed some light on allowed characters in emailadresses?

Grtz,
--
Rik Wasmus
Jun 24 '06 #5

P: n/a
Rik wrote:
[...]
check,
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i

Now, who can shed some light on allowed characters in emailadresses?


http://en.wikipedia.org/wiki/Email_address#Limitations

--
Bart

Jun 24 '06 #6

P: n/a
"Rik" <lu************@hotmail.com> writes:
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i

Allthough I'm not sure which characters are actually allowed in an
emailadress.
That's reason enough *not* to be too specific. A prefectly good looking
email like me@example.com will probably be wrong, whereas
me****@[192.168.10.2] might be correct (for another IP address at least).
Then again, I don't know where the validation is for, it could be
sufficient.


You can't validate an email adress perfectly just by looking at it
(i.e., check that it is an email address that will receive mail
for the correct person). The only one who knows that is the person
writing it. It's *really* annoying to have a correct email address
rejected by an overzealous validator, so it's better to err on the
side of caution. For that, I prefer just checking for "@" and "."
in that order, which will at least catch really bad errors.

As for the specification, mail addresses were specified in RFC 822,
and probably revised in RFC 2822. Here is someone who has tried
interpreting it: <URL:http://www.cs.tut.fi/~jkorpela/rfc/822addr.html>

/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.'
Jun 24 '06 #7

P: n/a
Rik
Bart Van der Donck wrote:
Rik wrote:
[...]
check,
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i

Now, who can shed some light on allowed characters in emailadresses?


http://en.wikipedia.org/wiki/Email_address#Limitations

Hmmz, is someone can elaborate on the _exact_ meaning of 'any hyphen of the
ascii character set'. Maybe it's the language difference, but if I translate
hyphen the only one i know is '-'....

So,
/^[0-9a-z!#$%&'*+\-\/=?^_`{|}~]+(\.*[0-9a-z!#$%&'*+\-\/=?^_`{|}~]+)*@([0-9a-
z\-]+\.)+[a-z]{1,6}$/i
Then again, let's not go as far as Friedl's tester, quoted emailadresses are
possible, a terrible, terrible thing to adjust for within limited code...

Grtz,
--
Rik Wasmus
Jun 24 '06 #8

P: n/a
Rik
Lasse Reichstein Nielsen wrote:
"Rik" <lu************@hotmail.com> writes:
/^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i

Allthough I'm not sure which characters are actually allowed in an
emailadress.


That's reason enough *not* to be too specific. A prefectly good
looking email like me@example.com will probably be wrong, whereas
me****@[192.168.10.2] might be correct (for another IP address at
least).
Then again, I don't know where the validation is for, it could be
sufficient.


You can't validate an email adress perfectly just by looking at it
(i.e., check that it is an email address that will receive mail
for the correct person). The only one who knows that is the person
writing it. It's *really* annoying to have a correct email address
rejected by an overzealous validator, so it's better to err on the
side of caution. For that, I prefer just checking for "@" and "."
in that order, which will at least catch really bad errors.


Well, then I'd at least do this:
/^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/
Checking for the disallowes starting/ending dots is relativly simple to
incorporate.
It all depends on where you are going to use the emailadress for I suppose.

Grtz,
--
Rik Wasmus
Jun 24 '06 #9

P: n/a
Rik wrote:
[...]

Well, then I'd at least do this:
/^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/

Checking for the disallowes starting/ending dots is relativly simple to
incorporate.
It all depends on where you are going to use the emailadress for I suppose.


There's much more to it. If you're planning to write a solid email
address validator, I think you should at least scope it down to the RFC
you wish to comply with. Nowadays it looks as if RFC2822 would be a
good choice.

Javascript and Perl regular expression syntaces are quite similar,
maybe the following could be a start:

http://search.cpan.org/~cwest/Email-...ail/Address.pm
(=for RFC2822)

--
Bart

Jun 24 '06 #10

P: n/a
Sorry but i didnt get much from the solution, im looking for the JS
equivalent of php's function: strstr ( $haystack, $needle ) which
returns false if the needle is not found in the haystack.

is there something i can use which looks like this:

if (strstr( form.email.value, '@') == FALSE) { return false }
Bart Van der Donck wrote:
Rik wrote:
[...]

Well, then I'd at least do this:
/^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/

Checking for the disallowes starting/ending dots is relativly simple to
incorporate.
It all depends on where you are going to use the emailadress for I suppose.


There's much more to it. If you're planning to write a solid email
address validator, I think you should at least scope it down to the RFC
you wish to comply with. Nowadays it looks as if RFC2822 would be a
good choice.

Javascript and Perl regular expression syntaces are quite similar,
maybe the following could be a start:

http://search.cpan.org/~cwest/Email-...ail/Address.pm
(=for RFC2822)

--
Bart


Jun 24 '06 #11

P: n/a
am i right in thinking indexof() is the baby for this

libsfan01 wrote:
Sorry but i didnt get much from the solution, im looking for the JS
equivalent of php's function: strstr ( $haystack, $needle ) which
returns false if the needle is not found in the haystack.

is there something i can use which looks like this:

if (strstr( form.email.value, '@') == FALSE) { return false }
Bart Van der Donck wrote:
Rik wrote:
[...]

Well, then I'd at least do this:
/^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/

Checking for the disallowes starting/ending dots is relativly simple to
incorporate.
It all depends on where you are going to use the emailadress for I suppose.


There's much more to it. If you're planning to write a solid email
address validator, I think you should at least scope it down to the RFC
you wish to comply with. Nowadays it looks as if RFC2822 would be a
good choice.

Javascript and Perl regular expression syntaces are quite similar,
maybe the following could be a start:

http://search.cpan.org/~cwest/Email-...ail/Address.pm
(=for RFC2822)

--
Bart


Jun 24 '06 #12

P: n/a
libsfan01 said the following on 6/24/2006 3:20 PM:
am i right in thinking indexof() is the baby for this


"baby for this"? Are you writing JS or delivering babies?

indexof will tell you if a character is in a string but it is a long way
from doing what you original asked about.
@.yourHouse

indexOf('@') ==> true
indexOf('.') ==> true

@.yourMama ==> Nowhere close to an email address.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 24 '06 #13

P: n/a
Lasse Reichstein Nielsen wrote:
You can't validate an email adress perfectly just by looking at it
(i.e., check that it is an email address that will receive mail
for the correct person). The only one who knows that is the person
writing it. It's *really* annoying to have a correct email address
rejected by an overzealous validator, so it's better to err on the
side of caution. For that, I prefer just checking for "@" and "."
in that order, which will at least catch really bad errors.


Even that will yield false positives in the trivial cases of mail
between two users on the same Unix system ("john") or two systems in the
same domain ("jane@payroll" -- the ability to abbreviate
"payroll.world-wide-wickets.com" to "payroll" in this way is the
historic reason that domains go right-to-left in the first place.)

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Jun 24 '06 #14

P: n/a
Rik
Bart Van der Donck wrote:
Javascript and Perl regular expression syntaces are quite similar,
maybe the following could be a start:

http://search.cpan.org/~cwest/Email-...ail/Address.pm
(=for RFC2822)


I'm actually more at home in PERL/PHP then javascript :-).
I'd have to look at tha package itself, maybe tomorrow. Then again, I saw
Friedl's solution for complete compliace with every possible valid
emailadress, and was in awe. I'm instantly giving up trying to get anywhere
near his solution. The damned thing is, when I try to find it again, I only
get references to his book, and my browser history has been cleaned in the
mean while....

Then again, maybe his book is a good purchase, really mastering regular
expressions is usually a time-saver in the first place, and rewriting them
when the reuirements change much simpler then huge quantaties of custom
code.

Grtz,
--
Rik Wasmus
Jun 24 '06 #15

P: n/a

libsfan01 wrote:
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


Dear sir,
Instead of all of this, you could just add a function:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@" != -1) &&
(document.form.email.value.indexOf("." != -1)
{
problem = 0;
}
else
{
problem = 1;
}
}

And then have the function return the value of problem. From there,
you can just have an alert popup if problem=1, and otherwise have it
run the form submission.

I have the honor to remain your most humble and Ob't Sv't in our war
against the King.

--
Patrick Reilly
1st Coy.
Colonel Seth Warner's Regiment

Jun 25 '06 #16

P: n/a
pe********************@gmail.com wrote on 25 jun 2006 in
comp.lang.javascript:
(document.form.email.value.indexOf("." != -1)


"." != -1

seems always to be true.

Does true have an index in a string?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 25 '06 #17

P: n/a
pe********************@gmail.com said the following on 6/25/2006 7:34 AM:
libsfan01 wrote:
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


Dear sir,
Instead of all of this, you could just add a function:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@" != -1) &&
(document.form.email.value.indexOf("." != -1)
{
problem = 0;
}
else
{
problem = 1;
}
}

And then have the function return the value of problem. From there,
you can just have an alert popup if problem=1, and otherwise have it
run the form submission.


And you run into the potential scenario I gave:

@.nowhere

Will return valid from your function even though it isn't even close to
a valid email address.

The only way to know an email address is valid for use is to email it
and receive a positive reply. Otherwise, it is just a semi-educated guess.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 25 '06 #18

P: n/a
JRS: In article <11**********************@c74g2000cwc.googlegroups .com>
, dated Sat, 24 Jun 2006 12:05:51 remote, seen in
news:comp.lang.javascript, libsfan01 <mc******@googlemail.com> posted :
Sorry but i didnt get much from the solution, im looking for the JS
equivalent of php's function: strstr ( $haystack, $needle ) which
returns false if the needle is not found in the haystack.

is there something i can use which looks like this:

if (strstr( form.email.value, '@') == FALSE) { return false }

There is. However, using == false or its equivalent is a sign of
fundamental inadequacy of understanding in most languages. In
javascript, so is (in almost all cases, including this one) attempting
string validation without using any RegExps.

Please read the newsgroup FAQ on the proper formatting of responses.
When validating E-mail addresses, where the necessary formats may change
during the life of the code, one should do no more than reject any which
it is thought cannot possibly be right - blank, for example. It may be
well only to warn if an address seems incorrect, allowing it to be used
if the user agrees.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of 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.
Jun 25 '06 #19

P: n/a
Dr John Stockton said the following on 6/25/2006 9:46 AM:
JRS: In article <11**********************@c74g2000cwc.googlegroups .com>
, dated Sat, 24 Jun 2006 12:05:51 remote, seen in
news:comp.lang.javascript, libsfan01 <mc******@googlemail.com> posted :
Sorry but i didnt get much from the solution, im looking for the JS
equivalent of php's function: strstr ( $haystack, $needle ) which
returns false if the needle is not found in the haystack.

is there something i can use which looks like this:

if (strstr( form.email.value, '@') == FALSE) { return false }

There is. However, using == false or its equivalent is a sign of
fundamental inadequacy of understanding in most languages.


More of this debate? And you missed telling him that FALSE != false

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 25 '06 #20

P: n/a
JRS: In article <11**********************@m73g2000cwd.googlegroups .com>
, dated Sun, 25 Jun 2006 04:34:14 remote, seen in
news:comp.lang.javascript, pe********************@gmail.com <pegasusflig
ht*********@gmail.com> posted :

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@" != -1) &&
Clearly untested, or else the missing parentheses would have been
detected.
(document.form.email.value.indexOf("." != -1)
{
problem = 0;
}
else
{
problem = 1;
}
}
For true/false values in legible programming one should use true and
false, not 0 and 1.

Your function sets an internal variable problem, and returns undefined.

Code should be indented to show logical structure; a unit of one
character is inadequate; and one should indent for more than just 'if'
statements.
The function that, for your algorithm, you should have written would go
more like

function checkEmail(Ctrl) { var Tmp = Ctrl.value
return (Tmp.indexOf("@") != -1) && (Tmp.indexOf(".") != -1) }

Your algorithm, however, is weak, it accepts ".@" and "@." and "@.@" for
example. One can do much better with a simple RegExp, as in the first
response (by BVdD).
Yours is a rather '70s style of programming ...
I have the honor to remain your most humble and Ob't Sv't in our war
against the King.


.... and that's 1770's.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of 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.
Jun 25 '06 #21

P: n/a

Randy Webb wrote:
pe********************@gmail.com said the following on 6/25/2006 7:34 AM:
libsfan01 wrote:
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


Dear sir,
Instead of all of this, you could just add a function:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@" != -1) &&
(document.form.email.value.indexOf("." != -1)
{
problem = 0;
}
else
{
problem = 1;
}
}

And then have the function return the value of problem. From there,
you can just have an alert popup if problem=1, and otherwise have it
run the form submission.


And you run into the potential scenario I gave:

@.nowhere

Will return valid from your function even though it isn't even close to
a valid email address.

The only way to know an email address is valid for use is to email it
and receive a positive reply. Otherwise, it is just a semi-educated guess.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Dear sir,
You could just simply add a couple of lines using charAt() determining
where the @ is placed and where the "." is placed, and you could even
change the "." indexOf() to lastIndexOf(), therefore eliminating the
possibility that a person has a dot in their username, instead of their
e-mail domain. As long as you can determine that the "@" and "." are
not right next to each other (using charAt() and some easy
subtraction), you can avoid the proposed problem with this. The result
would be something like:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1)
{
problem = 0;
}
else
{
problem = 1;
}
}

And again, as I said, just have something that alerts of an error if
problem returns 1, and if it returns 0 then it continues to process the
e-mail address.

I have the honor to remain your most humble and Ob't Sv't in our war
against the King.

--
Patrick Reilly
1st Coy.
Colonel Seth Warner's Regiment

Jun 26 '06 #22

P: n/a

pe********************@gmail.com wrote:
Randy Webb wrote:
pe********************@gmail.com said the following on 6/25/2006 7:34 AM:
libsfan01 wrote:
> 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

Dear sir,
Instead of all of this, you could just add a function:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@" != -1) &&
(document.form.email.value.indexOf("." != -1)
{
problem = 0;
}
else
{
problem = 1;
}
}

And then have the function return the value of problem. From there,
you can just have an alert popup if problem=1, and otherwise have it
run the form submission.


And you run into the potential scenario I gave:

@.nowhere

Will return valid from your function even though it isn't even close to
a valid email address.

The only way to know an email address is valid for use is to email it
and receive a positive reply. Otherwise, it is just a semi-educated guess.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Dear sir,
You could just simply add a couple of lines using charAt() determining
where the @ is placed and where the "." is placed, and you could even
change the "." indexOf() to lastIndexOf(), therefore eliminating the
possibility that a person has a dot in their username, instead of their
e-mail domain. As long as you can determine that the "@" and "." are
not right next to each other (using charAt() and some easy
subtraction), you can avoid the proposed problem with this. The result
would be something like:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1)
{
problem = 0;
}
else
{
problem = 1;
}
}

And again, as I said, just have something that alerts of an error if
problem returns 1, and if it returns 0 then it continues to process the
e-mail address.

I have the honor to remain your most humble and Ob't Sv't in our war
against the King.

--
Patrick Reilly
1st Coy.
Colonel Seth Warner's Regiment


Thank you Dr. Stockton for your reminder, as I did forget the
parenthesis to end the if qualifying expression. Corrected, the script
would look like this:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1))
{
problem = 0;
}
else
{
problem = 1;
}
}

And by the way, it is tested, and works perfectly well, I just
misstyped it.

I have the honor to remain your most humble and Ob't Sv't in our war
against the King.

--
Patrick Reilly
1st Coy.
Colonel Seth Warner's Regiment

Jun 28 '06 #23

P: n/a
pe********************@gmail.com wrote on 28 jun 2006 in
comp.lang.javascript:
function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1))
{
problem = 0;
}
else
{
problem = 1;
}
}


If ...indexOf("@") is not -1 and not 0 it must be >0

Starting a adres with a . seems strange, so also >0

The function seems not very useful, since the local variable "problem"
is not used inside the function and not available outside it.

try:

function checkEmail() {
var tmp = document.form.email
var tmpval = tmp.value
if ( (tmpval.indexOf(".") > 0 ) &&
(tmpval.indexOf("@") > 0 ) &&
(tmpval.lastIndexOf(".") > tmpval.indexOf("@") + 1) &&
(tmpval.lastIndexOf(".") < tmp.length) &&
(tmpval.indexOf(" ") == -1))
return 0;
return 1;
}

problem = checkEmail();

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 28 '06 #24

P: n/a

Evertjan. wrote:
pe********************@gmail.com wrote on 28 jun 2006 in
comp.lang.javascript:
function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1))
{
problem = 0;
}
else
{
problem = 1;
}
}


If ...indexOf("@") is not -1 and not 0 it must be >0

Starting a adres with a . seems strange, so also >0

The function seems not very useful, since the local variable "problem"
is not used inside the function and not available outside it.

try:

function checkEmail() {
var tmp = document.form.email
var tmpval = tmp.value
if ( (tmpval.indexOf(".") > 0 ) &&
(tmpval.indexOf("@") > 0 ) &&
(tmpval.lastIndexOf(".") > tmpval.indexOf("@") + 1) &&
(tmpval.lastIndexOf(".") < tmp.length) &&
(tmpval.indexOf(" ") == -1))
return 0;
return 1;
}

problem = checkEmail();

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Dear sir,
True, but I was trying to avoid using a sort of parameter or return
value. Please don't ask why.

I have the honor to remain your most humble and Ob't Sv't in our war
against the King.

--
Patrick Reilly
1st Coy.
Colonel Seth Warner's Regiment

Jun 29 '06 #25

P: n/a
JRS: In article <11*********************@y41g2000cwy.googlegroups. com>,
dated Wed, 28 Jun 2006 11:40:10 remote, seen in
news:comp.lang.javascript, pe********************@gmail.com <pegasusflig
ht*********@gmail.com> posted :
Lines: 128 pe********************@gmail.com wrote: Thank you Dr. Stockton for your reminder,
You are responding to yourself, and in a branch of the thread to which I
have not contributed.
as I did forget the
parenthesis to end the if qualifying expression. Corrected, the script
would look like this:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1))
{
problem = 0;
}
else
{
problem = 1;
}
}

And by the way, it is tested, and works perfectly well, I just
misstyped it.
It cannot "work perfectly well", as it has no external effect; it
returns undefined.

It is naive to believe that code which is re-typed will be free of new
error. If you cannot copy'n'paste tested code, then either don't post
"complete" code or mark it as re-typed. Moreover, YSCIB.

Read the newsgroup FAQ (previously cited in this thread) and/or the
references below on the appropriate formatting of Usenet responses. In
particular, superfluous material and signatures should not be quoted.
You have already been given, in this thread, a two-line version of your
algorithm, with proper input and output; and you have also been given a
better and similarly short algorithm using a RegExp. Why, then, do you
think your own contribution can be of any value?
--

DSS.

--
John Stockton, Surrey, UK. yyww merlyn demon co uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jun 29 '06 #26

P: n/a

Dr John Stockton wrote:
JRS: In article <11*********************@y41g2000cwy.googlegroups. com>,
dated Wed, 28 Jun 2006 11:40:10 remote, seen in
news:comp.lang.javascript, pe********************@gmail.com <pegasusflig
ht*********@gmail.com> posted :
Lines: 128
pe********************@gmail.com wrote:

Thank you Dr. Stockton for your reminder,


You are responding to yourself, and in a branch of the thread to which I
have not contributed.


You have contributed, my dear sir, because you have corrected my
mistake in typing.
as I did forget the
parenthesis to end the if qualifying expression. Corrected, the script
would look like this:

function checkEmail()
{
var problem;
if ((document.form.email.value.indexOf("@") != -1) &&
(document.form.email.value.indexOf(".") != -1) &&
(document.form.email.value.indexOf("@") != 0) &&
(document.form.email.value.lastIndexOf(".") >
document.form.email.value.indexOf("@") + 1) &&
(document.form.email.value.lastIndexOf(".") <
document.form.email.length) &&
(document.form.email.value.indexOf(" ") == -1))
{
problem = 0;
}
else
{
problem = 1;
}
}

And by the way, it is tested, and works perfectly well, I just
misstyped it.
It cannot "work perfectly well", as it has no external effect; it
returns undefined.


Now this, I explained previously. The person that asked for this
script, I said, would have to add a piece of code that would give an
alert if problem was equal to 1. If you wish, I could just change the
part with "problem = 0" and "problem = 1" to:
alert("This form was filled out correctly!");

and:
alert("This was incorrectly filled out, please try again.");
return false;

It is naive to believe that code which is re-typed will be free of new
error. If you cannot copy'n'paste tested code, then either don't post
"complete" code or mark it as re-typed. Moreover, YSCIB.

Read the newsgroup FAQ (previously cited in this thread) and/or the
references below on the appropriate formatting of Usenet responses. In
particular, superfluous material and signatures should not be quoted.
You have already been given, in this thread, a two-line version of your
algorithm, with proper input and output; and you have also been given a
better and similarly short algorithm using a RegExp. Why, then, do you
think your own contribution can be of any value?
Because any contribution can be of value, as one may not understand the
basis of the "short algorithm using a RegExp", and so they would be
more influenced to use the longer version because they understand it
better and would be able to modify it more easily than modifying
something they do not understand (fully). This could happen, so a
simplified version may be of consideration.
--

DSS.

--
John Stockton, Surrey, UK. yyww merlyn demon co uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.


I have the honor to remain your most humble and Ob't Sv't in our war
against the King.

--
Patrick Reilly
1st Coy.
Colonel Seth Warner's Regiment

Jun 30 '06 #27

This discussion thread is closed

Replies have been disabled for this discussion.