Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > validation stops running after 4 fields.

Reply
Thread Tools

validation stops running after 4 fields.

 
 
kevinfairs@googlemail.com
Guest
Posts: n/a
 
      11-29-2006
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.

 
Reply With Quote
 
 
 
 
Martijn Saly
Guest
Posts: n/a
 
      11-29-2006
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
 
Reply With Quote
 
 
 
 
kevinfairs@googlemail.com
Guest
Posts: n/a
 
      11-29-2006
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


 
Reply With Quote
 
kevinfairs@googlemail.com
Guest
Posts: n/a
 
      11-29-2006
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.



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


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-29-2006
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)
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-29-2006
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.


 
Reply With Quote
 
kevinfairs@googlemail.com
Guest
Posts: n/a
 
      11-29-2006
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:
> 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)


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-29-2006
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)
 
Reply With Quote
 
kevinfairs@googlemail.com
Guest
Posts: n/a
 
      11-29-2006
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?

 
Reply With Quote
 
kevinfairs@googlemail.com
Guest
Posts: n/a
 
      11-29-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ADAWARE hangs, stops, quits, disappears while running. hhawkins@cyberbeach.net Computer Support 1 07-29-2006 04:45 AM
Changing the user account of aspnet_wp, stops the programme from running? chortler@fetchmail.co.uk ASP .Net 2 05-05-2005 12:54 PM
Thread just stops running =?Utf-8?B?bWFyZWFs?= ASP .Net 9 03-09-2005 08:57 PM
Timer thread stops running Todd ASP .Net 4 04-08-2004 03:21 AM
show running returns nothing, routing stops Richard Antony Burton Cisco 10 11-25-2003 10:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57