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

Neophyte question

P: n/a
fox
Hi,

Looking through this forum, I cannot help to think that I am way over my
head. Meanwhile, I have a simple purpose and a $15 book on javascript.
Needless to say I need a bit of help. Can someone tell me why this
seems to consider every entered string to be "false" even when it is a
legit email address?

<script language="Javascript" Type="text/javascript">
function FrontPage_Form1_Validator(theForm)
{

if (!(theForm.email.value.indexOf(".")) 3 || !
(theForm.email.value.indexOf("@")) 0);
{
alert("You did not enter a valid email address for the 1 \"email\"
field.");
theForm.email.focus();
return (false);
}

return (true);
}

</script>
Thanks to anyone who might be able to correct my problem,
Fox
Sep 16 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ASM
fox a écrit :
Hi,

Looking through this forum, I cannot help to think that I am way over my
head. Meanwhile, I have a simple purpose and a $15 book on javascript.
Needless to say I need a bit of help. Can someone tell me why this
seems to consider every entered string to be "false" even when it is a
legit email address?
somme errors with your ( ) and more
<script language="Javascript" Type="text/javascript">
function FrontPage_Form1_Validator(theForm)
{

if (!(theForm.email.value.indexOf(".")) 3 || !
if( !( theForm.email.value.indexOf('.') 3)

if not ( point position in email is after 4th place )

And perso, I have an address with point in 2nd position (only a lettre
before) and another address with no point before @
I'lln't be accepted ... :-(
(theForm.email.value.indexOf("@")) 0 );
!( theForm.email.value.indexOf("@") 0 ) // without ';' in end
// because instruction
// continues

rest is OK
Thanks to anyone who might be able to correct my problem,
function FrontPage_Form1_Validator(theForm)
{
if ( !(theForm.email.value.indexOf(".") 3) ||
!(theForm.email.value.indexOf("@") 0) )
{
alert("You did not enter a valid email address for" +
" the \"email\" field.");
theForm.email.focus();
return false;
}
return true;
}
function FrontPage_Form1_Validator(theForm)
{
var mail = theForm.email.value;
if((mail.indexOf(".") < 0) ||
(mail.indexOf("@") < 0) ||
(mail.length < 8))
{
alert("You did not enter a valid email address for" +
" the \"email\" field.");
theForm.email.focus();
return false;
}
return true;
}
Sep 16 '06 #2

P: n/a
fox wrote on 16 sep 2006 in comp.lang.javascript:
Hi,

Looking through this forum, I cannot help to think that I am way over
my head. Meanwhile, I have a simple purpose and a $15 book on
javascript. Needless to say I need a bit of help. Can someone tell me
why this seems to consider every entered string to be "false" even
when it is a legit email address?

<script language="Javascript" Type="text/javascript">
Do not use language="Javascript", [deprecated]
function FrontPage_Form1_Validator(theForm)
Why use Frontpage, it degrades your learning curve IMHO
{

if (!(theForm.email.value.indexOf(".")) 3
If a value is NOT-ed it is bolean and cannot be compared with 3
|| ! (theForm.email.value.indexOf("@")) 0);
same!

And the ; should not be there, as it mankes the "then" of the if clause
an empty statement. [this is your main error, I think!]

if
(
!(theForm.email.value.indexOf(".") 3 )
||
!(theForm.email.value.indexOf("@") 0 )
)

however:

!(theForm.email.value.indexOf(".") 3 )

is the same as:

(theForm.email.value.indexOf('.') <= 3 )

and

!(theForm.email.value.indexOf("@") 0 )

is the same as:

(theForm.email.value.indexOf('@') = 0 )

{
alert("You did not enter a valid email address for the 1 \"email\"
field.");
Use ' and ", simpler:

alert('You did not enter a valid email address for the 1 "email"
field.');

theForm.email.focus();
return (false);
return false;
}

return (true);
return true;
}

</script>
================================

Together:

<script type='text/javascript'>

function My_Form1_Validator(theForm) {
if ((theForm.email.value.indexOf('.') <= 3 ) ||
(theForm.email.value.indexOf('@') = 0 ) ) {
alert('You did ... for the 1 "email" field.');
theForm.email.focus();
return false;
}
return true;
}

</script>

=====================

However, you could use regular expressions for the testing:

<script type='text/javascript'>

function My_Form1_Validator(theForm) {
if (/^.{0,2}\./.test(theForm.email.value) ||
/[^@]/.test(theForm.email.value) ) {
alert('You did ... for the 1 "email" field.');
theForm.email.focus();
return false;
}
return true;
}

</script>
Nothing tested.

The reason for not wanting a period in the first 3 places of an
emailaddress escapes me.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 16 '06 #3

P: n/a
fox wrote:
Looking through this forum, I cannot help to think that I am way over my
head. Meanwhile, I have a simple purpose and a $15 book on javascript.
Needless to say I need a bit of help. Can someone tell me why this
seems to consider every entered string to be "false" even when it is a
legit email address?

<script language="Javascript" Type="text/javascript">
function FrontPage_Form1_Validator(theForm)
{

if (!(theForm.email.value.indexOf(".")) 3
Err. "If the first . character in the email address is not at least the
third character"? So j.*****@example.com isn't an acceptable email address?
|| !
(theForm.email.value.indexOf("@")) 0);
The semi-colon here ends the statement which makes ...
{
alert("You did not enter a valid email address for the 1 \"email\"
field.");
.... et al a simple block (or possibly an anonymous object, I'm a little
unclear on some of the finer points of JS syntax) rather then part of the
if.

--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 16 '06 #4

P: n/a
JRS: In article <45**********************@news.orange.fr>, dated Sat, 16
Sep 2006 20:40:20 remote, seen in news:comp.lang.javascript, ASM
<st*********************@wanadoo.fr.invalidposte d :
>function FrontPage_Form1_Validator(theForm)
{
var mail = theForm.email.value;
if((mail.indexOf(".") < 0) ||
(mail.indexOf("@") < 0) ||
(mail.length < 8))
{
alert("You did not enter a valid email address for" +
" the \"email\" field.");
theForm.email.focus();
return false;
}
return true;
}

IMHO, much better tested with a RegExp : see in
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>

AFAICS, the code above will allow '....@@@@' . Consider :-

function FPFV(F) {
var OK = /^.+@.+\...+$/.test(F.email.value)
if (!OK) { alert("bad") ; F.email.focus() }
return OK }

and possible improvements by replacing in the RegExp the "wild" dots
with something more specific - [^@] for initial example

It's a good idea to read the newsgroup and its FAQ.
--
© 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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 16 '06 #5

P: n/a
fox <fo*@connexions.netwrote in news:Xns98408C81D50DCfoxconnexionsnet@
65.32.5.121:
Looking through this forum, I cannot help to think that I am way over
my
head. Meanwhile, I have a simple purpose and a $15 book on javascript.
Needless to say I need a bit of help. Can someone tell me why this
seems to consider every entered string to be "false" even when it is a
legit email address?
I can understand your desire to learn Javascript and your present attempt
at writing a useful script. However, I would like to point out that, in
general, it is easier and quicker to find somebody else's code that does
the same thing, and has already been through debugging and is known to
work. (Even if you can find something that *almost* does what you want,
you'll probably save time by editing that script, as opposed to starting
from scratch.) In your case, "validating an email address" has been done
already, dozens of times (maybe hundreds).

Although you won't have that satisfied feeling "I wrote that!" you will
speed up your development process a lot.
Sep 17 '06 #6

P: n/a
Jim Land wrote:
fox wrote:
>Looking through this forum, I cannot help to think that
I am way over my head. Meanwhile, I have a simple purpose
and a $15 book on javascript. Needless to say I need a bit
of help. Can someone tell me why this seems to consider
every entered string to be "false" even when it is a
legit email address?

I can understand your desire to learn Javascript and your
present attempt at writing a useful script. However, I
would like to point out that, in general, it is easier and
quicker to find somebody else's code that does the same
thing, and has already been through debugging and is known
to work. (Even if you can find something that *almost* does
what you want, you'll probably save time by editing that
script, as opposed to starting from scratch.) In your case,
"validating an email address" has been done already, dozens
of times (maybe hundreds).

Although you won't have that satisfied feeling "I wrote that!"
you will speed up your development process a lot.
Would that speed up development work? In principle nobody should be
employed to write javascript without having learnt it first so time
spent learning it would not be development work. In practice employers
may as people without javascript skills to write javascript, but then
they must expect them to be learning as they go along, and so expecting
relatively slow development. And ultimately anyone employed to write
javascript is going to be asked to do something that they cannot find an
example of (possibly because it is not possible), at which point they
will have no choice but learn to write it for themselves. For any
individual the time needed to learn javascript will not be significantly
different whenever they do it, but if they have been producing
javascript for some time the expectations of there employer may be such
that stopping to learn the subject for real, mid project, is
inconvenient to say the least.

Also, development time is only part of the picture. With at least 90% of
published javascript being atrociously poor a strategy of adapting it an
using it is likely to produce more poor code, with all the usual
implications for ongoing maintenance (and consequential associated
expense). Saving development time at a disproportional cost in
maintenance is doing nobody any favours.

Richard.
Sep 17 '06 #7

P: n/a

fox wrote:
Hi,

Looking through this forum, I cannot help to think that I am way over my
head. Meanwhile, I have a simple purpose and a $15 book on javascript.
Needless to say I need a bit of help. Can someone tell me why this
seems to consider every entered string to be "false" even when it is a
legit email address?

<script language="Javascript" Type="text/javascript">
function FrontPage_Form1_Validator(theForm)
{

if (!(theForm.email.value.indexOf(".")) 3 || !
(theForm.email.value.indexOf("@")) 0);
{
alert("You did not enter a valid email address for the 1 \"email\"
field.");
theForm.email.focus();
return (false);
}

return (true);
}

</script>
Thanks to anyone who might be able to correct my problem,
Fox
Is there any particular reason you don't want to use a Regular
Expression?

Surely it would be less code and time to use one?

Sep 17 '06 #8

P: n/a
JRS: In article <Xn**********************************@216.168.3.44 >,
dated Sun, 17 Sep 2006 03:20:26 remote, seen in
news:comp.lang.javascript, Jim Land <Rr**********@hotmail.composted :
>
I can understand your desire to learn Javascript and your present attempt
at writing a useful script. However, I would like to point out that, in
general, it is easier and quicker to find somebody else's code that does
the same thing, and has already been through debugging and is known to
work.
With Internet Search Engines, it is even more easy and quick to find
someone else's code that attempts the same thing and does it badly. The
immediate project may be "completed" faster, but may by false rejections
lose customers or by false acceptances waste company resources.

E-address validation code, for example, that does not allow . before @
or restricts the TLD to 2/3 characters. Or code which is, for its
length and complexity, unnecessarily liberal, allowing for example more
than one @ .

Only for those with a certain amount of skill and pessimism is an
unguided search likely to be efficient - using a well-maintained FAQ is
good, for example, but using a Web site with an ill-edited collection of
old answers is not.

It's a good idea to read the newsgroup and its FAQ.
--
© 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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.