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

validation stops running after 4 fields.

P: n/a
I've got the following javascript validation routine which runs against
an asp page.

function validate(form)
{
if (form.Forename.value == "") {
alert("Please enter a forename");
form.Forename.focus();
return false;
}
if (form.surname.value == "" ) {
alert("Please enter a surname");
form.surname.focus();
return false;
}
if (form.username.value == "" ) {
alert("Please enter a username");
form.username.focus();
return false;
}
if (form.jobtitle.value == "" ) {
alert("Please enter a job title");
form.jobtitle.focus();
return false;
}
if (form.e-mail.value == "" ) {
alert("Please enter an e-mail");
form.e-mail.focus();
return false;
}
if (form.nino.value == "" ) {
alert("Please enter a National Insurance number");
form.nino.focus();
return false;
}
if (form.payroll.value == "" ) {
alert("Please enter a payroll number");
form.payroll.focus();
return false;
}
if (form.FTE.value == "" ) {
alert("Please enter an FTE value");
form.FTE.focus();
return false;
}
if (form.password.value == "" ) {
alert("Please enter a password");
form.password.focus();
return false;
}
}

with my form's header being this:
<form name="data" action="new_user_submit.asp" onSubmit="return
validate(this)">

The field names are correct (copy and paste from the form) so it's not
that. They are all text inputs.

The validation will run through the first 4 fields, then simply let the
form submit regardless of the entries in the other fields.

Any suggestions as to why this is happening? or more importantly, how I
can get round it.

Nov 29 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
ke********@googlemail.com wrote:
I've got the following javascript validation routine which runs against
an asp page.

<snip>

with my form's header being this:
<form name="data" action="new_user_submit.asp" onSubmit="return
validate(this)">

The field names are correct (copy and paste from the form) so it's not
that. They are all text inputs.

The validation will run through the first 4 fields, then simply let the
form submit regardless of the entries in the other fields.

Any suggestions as to why this is happening? or more importantly, how I
can get round it.
Does it display errors or warnings in the javascript console?
Did you run your script through JSLint just to make sure?
--
Martijn Saly
Nov 29 '06 #2

P: n/a
No errors in the console when running, it just functions as expected,
but then allows submission after 4 fields (it doesn't matter whcih
order they are in, I've tried shifting them around to get different
validation orders), and no, I've not run it through JSLint to check it.

Martijn Saly wrote:
>
Does it display errors or warnings in the javascript console?
Did you run your script through JSLint just to make sure?
--
Martijn Saly
Nov 29 '06 #3

P: n/a
I have now run it through jslint, and the only errors it complains of
are the use of e-mail in the field name (which I have changed now to be
email rather than e-mail), and the == in the test which should be ===,
so I've changed that one as well.

still fails with the same problem though.

ke********@googlemail.com wrote:
No errors in the console when running, it just functions as expected,
but then allows submission after 4 fields (it doesn't matter whcih
order they are in, I've tried shifting them around to get different
validation orders), and no, I've not run it through JSLint to check it.

Martijn Saly wrote:

Does it display errors or warnings in the javascript console?
Did you run your script through JSLint just to make sure?
--
Martijn Saly
Nov 29 '06 #4

P: n/a
ke********@googlemail.com wrote on 29 nov 2006 in comp.lang.javascript:
if (form.e-mail.value == "" ) {
alert("Please enter an e-mail");
form.e-mail.focus();
return false;
}
Read your errors!

wrong:

<form>
<input name='e-mail' value='hi@low'>
</form>
<script type='text/javascript'>
alert(document.forms[0].e-mail.value)
</script>

right:

<form>
<input name='e-mail' value='hi@low'>
</form>
<script type='text/javascript'>
alert(document.forms[0].elements['e-mail'].value)
</script>
========================

Why not do this [not tested]:

<script type='text/javascript'>

var a = [
[' forename','Forename'], // 'f'orename???
[' surname','surname'],
[' username','username'],
[' job title','jobtitle'],
['n e-mail address','e-mail'],
[' National Insurance number','nino'],
[' payroll number','payroll'],
[' FTE value','FTE'],
[' password','password']
]

function validate(form) {

for (i=0; i<a.length; i++)
if (form.elements[a[i][1]].value == '' ) {
alert('Please enter a'+a[i][0]);
form.elements[a[i][1]].focus();
return false;
}

}

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 29 '06 #5

P: n/a
ke********@googlemail.com wrote:
<snip>
if (form.e-mail.value == "" ) {
<snip ^

It is unlikely that an attempt to subtract mail.value from forms.e will
be successful.

Richard.
Nov 29 '06 #6

P: n/a
Thanks.

Have attempted that, and exactly the same thing happens - it stops
validating fields after 4.

I've renamed the e-mail field to be email and, also replaced all the
correspoinding lines in the script with the new names.

As it's running the same code for all fields now, it has to be in the
form makeup?

form as below:
<form name="data" action="new_user_submit.asp" onSubmit="return
validate(this)">
<center>
<table border="1" width="75%">
<tr bgcolor="#b0c4de"><td colspan = '2'><center><h1>New User
Form</h1></td></tr>
<tr><td>Forename<td><center><input type='Text' name='Forename'
size='50'></center></td></tr>
<tr><td>Surname<td><center><input type='Text' name='surname'
size='50'></center></td></tr>
<tr><td>Username<td><center><input type='Text' name='username'
size='50'></center></td></tr>
<tr><td>Password<td><center><input type='Text' name='password'
size='50'></center></td></tr>
<tr><td>E-mail<td><center><input type='Text' name='email'
size='50'></center></td></tr>
<tr><td>Job Title<td><center><input type='Text' name='jobtitle'
size='50'></center></td></tr>
<tr><td>National Insurance Number<td><center><input type='Text'
name='nino' size='50'></center></td></tr>
<tr><td>Payroll Number<td><center><input type='Text' name='payroll'
size='50'></center></td></tr>
<tr><td>FTE<td><center><input type='Text' name='FTE'
size='50'></center></td></tr>
<br>
<input type="submit">
</p>
</form>
any further ideas?
Evertjan. wrote:
ke********@googlemail.com wrote on 29 nov 2006 in comp.lang.javascript:
if (form.e-mail.value == "" ) {
alert("Please enter an e-mail");
form.e-mail.focus();
return false;
}

Read your errors!

wrong:

<form>
<input name='e-mail' value='hi@low'>
</form>
<script type='text/javascript'>
alert(document.forms[0].e-mail.value)
</script>

right:

<form>
<input name='e-mail' value='hi@low'>
</form>
<script type='text/javascript'>
alert(document.forms[0].elements['e-mail'].value)
</script>
========================

Why not do this [not tested]:

<script type='text/javascript'>

var a = [
[' forename','Forename'], // 'f'orename???
[' surname','surname'],
[' username','username'],
[' job title','jobtitle'],
['n e-mail address','e-mail'],
[' National Insurance number','nino'],
[' payroll number','payroll'],
[' FTE value','FTE'],
[' password','password']
]

function validate(form) {

for (i=0; i<a.length; i++)
if (form.elements[a[i][1]].value == '' ) {
alert('Please enter a'+a[i][0]);
form.elements[a[i][1]].focus();
return false;
}

}

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 29 '06 #7

P: n/a
ke********@googlemail.com wrote on 29 nov 2006 in comp.lang.javascript:
Have attempted that, and exactly the same thing happens - it stops
validating fields after 4.

I've renamed the e-mail field to be email
not necessary, you should use

forms['myForm'].elements['myElement']

or

function validate(thisForm) {
.....
thisForm.elements['myElement']

anyway for safety and clarity.
and, also replaced all the
correspoinding lines in the script with the new names.

As it's running the same code for all fields now, it has to be in the
form makeup?
Please make a as short as possible complete example where the phenomenon
still happens, and preferably put that on a test page, giving the url.

Or you could try to debug it yourself,
by reading and reporing error texts and adding alerts like this:

function validate(form) {

for (i=0; i<a.length; i++) {

// ===============
alert(i)
alert(a[i][0])
alert(form.elements[a[i][1]].value)
// ===============

if (form.elements[a[i][1]].value == '' ) {
alert('Please enter a'+a[i][0]);
form.elements[a[i][1]].focus();
return false;
}
}
}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 29 '06 #8

P: n/a
It appears to run correctly, until it hits the fourth if condition,
then simply submit the form. it even reads the value in the form, and
attempts to process the if condition.

there's not a limit on the number of it's or something like that is
there?

Nov 29 '06 #9

P: n/a
The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.

many thanks for help on this.

Nov 29 '06 #10

P: n/a
ke********@googlemail.com wrote on 29 nov 2006 in comp.lang.javascript:
The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.
again:

Please always quote on usenet.
This is not email.
Not all news servers and all news readers work like yours.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 29 '06 #11

P: n/a
ke********@googlemail.com wrote:
The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.

many thanks for help on this.
I wonder why, since form.e-mail was also wrong. So now you know why I
pointed you to jslint so you'll find the error yourself, and you might
actually learn something from it ;)

Anyway, why password is not good, I can't explain. I always use
"password" as a field name for a password. And not only because Firefox
and Opera recognize it for quickly logging on.

I guess something else is wrong. Have you looked at the FTE field while
realizing that javascript is case-sensetive?...

--
Martijn Saly
Nov 29 '06 #12

P: n/a
Lee
ke********@googlemail.com said:
>
The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.
I find that hard to believe.
Did you also rename your "e-mail" field at the same time?
--

Nov 29 '06 #13

P: n/a
Martijn Saly wrote:
ke********@googlemail.com wrote:
>The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.

many thanks for help on this.

I wonder why, since form.e-mail was also wrong. So now you know why I
pointed you to jslint so you'll find the error yourself, and you might
actually learn something from it ;)
<snip>

Wrong in terms of being likely to result in a runtime error, but not
actually a syntax error, so not something jslint is certain to
identify.

Richard.

Nov 29 '06 #14

P: n/a
Richard Cornford wrote:
Martijn Saly wrote:
>ke********@googlemail.com wrote:
>>The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.

many thanks for help on this.
I wonder why, since form.e-mail was also wrong. So now you know why I
pointed you to jslint so you'll find the error yourself, and you might
actually learn something from it ;)
<snip>

Wrong in terms of being likely to result in a runtime error, but not
actually a syntax error, so not something jslint is certain to
identify.

Richard.
Hm, I guess you got me there :)

--
Martijn Saly
Nov 30 '06 #15

P: n/a
In comp.lang.javascript message
<11**********************@l12g2000cwl.googlegroups .com>, Wed, 29 Nov
2006 00:34:28, "ke********@googlemail.com" <ke********@googlemail.com>
wrote:
>
function validate(form)
{
if (form.Forename.value == "") {
alert("Please enter a forename");
form.Forename.focus();
return false;
}
... ...
(1) That allows a forename of whitespace; better to use
OK = /\S/.test()
(2) A forename must come first. A surname can come first. One should
use such as either "first" and "last" or "family" and "given".
(3) I consider it unwise to use, as an identifier, a common javascript
word; sometimes it causes problems. Call it F or Frm or Aform to be
safer. Of course, those who have a perfect understanding of all present
and future browsers will know immediately in all cases what is safe and
what is not.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old 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.
Nov 30 '06 #16

P: n/a
Dr J R Stockton said the following on 11/30/2006 7:14 AM:
In comp.lang.javascript message
<11**********************@l12g2000cwl.googlegroups .com>, Wed, 29 Nov
2006 00:34:28, "ke********@googlemail.com" <ke********@googlemail.com>
wrote:
>>
function validate(form)
{
if (form.Forename.value == "") {
alert("Please enter a forename");
form.Forename.focus();
return false;
}
... ...

(1) That allows a forename of whitespace; better to use
OK = /\S/.test()
(2) A forename must come first. A surname can come first. One should
use such as either "first" and "last" or "family" and "given".
(3) I consider it unwise to use, as an identifier, a common javascript
word; sometimes it causes problems.
"form" is not a common javascript "word" though. forms is with an s but
without it is just another name.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 30 '06 #17

P: n/a
Yes, but I did them one at a time, password first, then e-mail

I could see why the e-mail would fail, bas e can't subtract mail. but
the password one has me bafflled.

never mind - it works for what I require anyhow.
Lee wrote:
ke********@googlemail.com said:

The problem was in the word "password" as a field name. renaming this
to be pwd has cleared my problems.

I find that hard to believe.
Did you also rename your "e-mail" field at the same time?
--
Dec 1 '06 #18

P: n/a
In comp.lang.javascript message <R6********************@telcove.net>,
Thu, 30 Nov 2006 16:34:17, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 11/30/2006 7:14 AM:
>In comp.lang.javascript message <1164789268.676235.238800@l12g2000cw
l.googlegroups.com>, Wed, 29 Nov 2006 00:34:28,
"ke********@googlemail.com" <ke********@googlemail.com wrote:
>>>
function validate(form)
{
if (form.Forename.value == "") {
alert("Please enter a forename");
form.Forename.focus();
return false;
}
... ...
(1) That allows a forename of whitespace; better to use
OK = /\S/.test()
(2) A forename must come first. A surname can come first. One
should use such as either "first" and "last" or "family" and "given".
(3) I consider it unwise to use, as an identifier, a common
javascript word; sometimes it causes problems.

"form" is not a common javascript "word" though. forms is with an s but
without it is just another name.

True. But javascript is often embedded in HTML containing <form ...>,
and that certainly can cause problems to a programmer seeking all places
where the identifier 'form' is used.

At one time I named forms as "FormA", "Form1", "Form2" etc.; that's safe
coding, but if searching the site for form-names I don't want to find,
or to think about not finding, the plain-text word "Format". So I now
call them "FrmA" etc.; the triliteral "frm" is uncommon in English.

Nevertheless, my (3) is still exactly true - it does not actually *say*
that the OP did that.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Dec 1 '06 #19

P: n/a
Dr J R Stockton said the following on 12/1/2006 7:00 AM:
In comp.lang.javascript message <R6********************@telcove.net>,
Thu, 30 Nov 2006 16:34:17, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 11/30/2006 7:14 AM:
>>In comp.lang.javascript message <1164789268.676235.238800@l12g2000cw
l.googlegroups.com>, Wed, 29 Nov 2006 00:34:28,
"ke********@googlemail.com" <ke********@googlemail.com wrote:
function validate(form)
{
if (form.Forename.value == "") {
alert("Please enter a forename");
form.Forename.focus();
return false;
}
... ...
(1) That allows a forename of whitespace; better to use
OK = /\S/.test()
(2) A forename must come first. A surname can come first. One
should use such as either "first" and "last" or "family" and "given".
(3) I consider it unwise to use, as an identifier, a common
javascript word; sometimes it causes problems.
"form" is not a common javascript "word" though. forms is with an s but
without it is just another name.


True. But javascript is often embedded in HTML containing <form ...>,
True.
and that certainly can cause problems to a programmer seeking all places
where the identifier 'form' is used.
Yes, but that is not what you said. You said "a common javascript word"
and the word "form" does not fit that criteria.
At one time I named forms as "FormA", "Form1", "Form2" etc.; that's safe
coding, but if searching the site for form-names I don't want to find,
or to think about not finding, the plain-text word "Format". So I now
call them "FrmA" etc.; the triliteral "frm" is uncommon in English.

Nevertheless, my (3) is still exactly true - it does not actually *say*
that the OP did that.
Anything you say with regards to what you think will always be "exactly
true" unless you change your mind so I am not sure what point you were
trying to make there.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 1 '06 #20

P: n/a
In comp.lang.javascript message <c8********************@telcove.net>,
Fri, 1 Dec 2006 16:22:45, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 12/1/2006 7:00 AM:
>In comp.lang.javascript message <R6********************@telcove.net>,
Thu, 30 Nov 2006 16:34:17, Randy Webb <Hi************@aol.comwrote:
>>Dr J R Stockton said the following on 11/30/2006 7:14 AM:
In comp.lang.javascript message <1164789268.676235.238800@l12g2000cw
l.googlegroups.com>, Wed, 29 Nov 2006 00:34:28,
"ke********@googlemail.com" <ke********@googlemail.com wrote:
function validate(form)
>>>(3) I consider it unwise to use, as an identifier, a common
javascript word; sometimes it causes problems.
"form" is not a common javascript "word" though. forms is with an s but
without it is just another name.
>and that certainly can cause problems to a programmer seeking all places
where the identifier 'form' is used.

Yes, but that is not what you said. You said "a common javascript word"
and the word "form" does not fit that criteria.
The singular form is "criterion". But I did not actually say that
"form" did fit.

The presence of "form" certainly made me think of point (3). However,
if I had started to write, say, "a common javascript word such as
'form'", I would then have considered whether that was a correct
statement.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borland.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.htmlff. with care.
Dec 2 '06 #21

P: n/a
Dr J R Stockton said the following on 12/2/2006 2:10 PM:
In comp.lang.javascript message <c8********************@telcove.net>,
Fri, 1 Dec 2006 16:22:45, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 12/1/2006 7:00 AM:
>>In comp.lang.javascript message <R6********************@telcove.net>,
Thu, 30 Nov 2006 16:34:17, Randy Webb <Hi************@aol.comwrote:
Dr J R Stockton said the following on 11/30/2006 7:14 AM:
In comp.lang.javascript message <1164789268.676235.238800@l12g2000cw
l.googlegroups.com>, Wed, 29 Nov 2006 00:34:28,
"ke********@googlemail.com" <ke********@googlemail.com wrote:
>function validate(form)
>>>>(3) I consider it unwise to use, as an identifier, a common
javascript word; sometimes it causes problems.
"form" is not a common javascript "word" though. forms is with an s but
without it is just another name.
>>and that certainly can cause problems to a programmer seeking all places
where the identifier 'form' is used.

Yes, but that is not what you said. You said "a common javascript
word" and the word "form" does not fit that criteria.

The singular form is "criterion".
Whatever.
But I did not actually say that "form" did fit.
Trying to have a common sense conversation with you is an exercise in
futility.
The presence of "form" certainly made me think of point (3).
The presence of "form" made you make the statement. Can't you at least
be honest enough to admit and deal with that?
However, if I had started to write,
Let me apply your reasoning to that. You didn't "write" anything, you
typed it!

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 2 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.