...

View Full Version : Validation Script won't work in Mozilla! Why?



Daedalus
02-11-2004, 11:43 PM
Hi I hope someone can help me,

I'm a complete Javascript novice and I have pieced together a validation script for a form using examples.

I have attached both the webpage and .js file for anyone to take a look at... My problem is that the script only seems to work, (produce an error message when an invalid selection is made), in Internet Explorer and Opera. When using any varient of Mozilla, such as Netscape or FireFox the script fails to operate.

The beginning of the Form in the HTML page looks like this:

<form name="rqstForm" action="./makerequest.pl" onSubmit="validate();" method=GET>

and the Javascript is:

function validate() {
checkProgramme();
checkCapacity();
checkWeeks();
}

function checkProgramme() {
if (rqstForm.programme.value != "select") {
}
else {
alert('You must select a programme.');
return false;
}
}

function checkCapacity() {
if ((rqstForm.capacity.value >0) && (rqstForm.capacity.value <501)) {
}
else {
alert('Capacity must be between 1 and 500.');
return false;
}
}

function checkWeeks() {
if (rqstForm.endweek.value >= rqstForm.startweek.value) {
}
else {
alert('The end week must be later than the start week.');
return false;
}
}

I appologise for the untidyness of the HTML, however it was put together in a bit of a hurry and is dynamically generated by a Perl file when accessed online. The beginning of the Form is shown by a comment in the HTML file to make it easier to find.

If someone could help me fix this problem it would be very much appreciated!

Thank you.

glenngv
02-12-2004, 04:10 AM
You are referencing the form incorrectly which IE allows :rolleyes:
This is the correct way:

document.rqstForm

but you can pass the form reference from the onsubmit using the keyword this which refers to the current element that is the form.

<form onsubmit="return validate(this)"

and you must return true or false to the form onsubmit handler to determine if submission will continue or not.


function validate(objForm) {
return checkProgramme(objForm) && checkCapacity(objForm) && checkWeeks(objForm);
}

function checkProgramme(objForm) {
if (objForm.programme.value != "select") {
...

Then each of the functions inside validate() must return true or false. true denotes no input error, while false otherwise.

Check the javascript console so that you can see the details of the error.

Daedalus
02-12-2004, 10:46 AM
Thank you very much!

Roy Sinclair
02-12-2004, 03:00 PM
Originally posted by glenngv
You are referencing the form incorrectly which IE allows :rolleyes:
This is the correct way:

document.rqstForm

...


Must have been in a hurry when you posted that one :).


Daedalus,

The correct forms are document.forms.rqstForm or document.forms['rqstForm']. Either is valid and will work cross browser, the reason IE accepts the document.rqstForm and rqstForm by itself is because it pollutes the global namespace with every object that's given a name or an id. Excepting Opera which is trying to be as compatible as possible the rest of the browsers keep the global namespace clear since all those objects have proper places in the object model where they can be found.

Daedalus
02-12-2004, 04:33 PM
Thank you, very much appreciated... haven't had a chance to edit things yet, but I will do it tonight.

Daedalus
02-12-2004, 07:12 PM
Thanks guys, got it working perfectly now!

glenngv
02-13-2004, 01:03 AM
Originally posted by Roy Sinclair
Must have been in a hurry when you posted that one :).


No. :p
document.rqstForm is also valid and working. Though using the forms collection is the more proper way.

Try this simple example and then tell me which browser this is not working.

<html>
<body>
<form name="myform">
<input type="button" value="What is document.myform?" onclick="alert(document.myform)" />
</form>
</body>
</html>

Roy Sinclair
02-13-2004, 02:31 PM
I stand corrected. It works in all the browsers I have. I probably won't use it myself since adding the .forms. does make it clear what I'm trying to use.

liorean
02-13-2004, 05:27 PM
Glenngv: Yeah, but how about something like this?

<form id="whatever">
&nbsp;&nbsp;<input name="whatever">
</form>

<form id="whatever">
&nbsp;&nbsp;<input name="forms" id="whatever">
</form>

<form name="whatever">
&nbsp;&nbsp;<input name="whatever" id="forms">
</form>

<form name="elements">
&nbsp;&nbsp;<input name="elements" id="forms">
</form>



(Note that for the form element the id and name attributes share namespace, but for the form controls id and name have separate namespaces. Id is still limited to one per document, but name is limited to a single control or set of controls within the same form element)

beetle
02-13-2004, 07:54 PM
Furthermore, liorean, XHTML doesn't even allow the name attribute for form elements.

liorean
02-13-2004, 08:00 PM
Well, Strict doesn't. Transitional and Frameset does.

beetle
02-13-2004, 08:07 PM
Right ;)

Of course, I made fValidate (http://www.peterbailey.net/fValidate) for a reason :D

liorean
02-13-2004, 08:11 PM
Oh, a "Welcome back!" might be appropriate. I've missed our occasional discussions here.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum