form validation script not working

M

Matt Herson

I am using the following script given in an earlier post:

<script type="text/javascript">
function validate(form) {
for (var i = 0; i < form.elements.length; i++) {
var elem = form.elements;
if (elem.tagName == "INPUT" &&
(elem.type == "text" || elem.type == "password") ||
elem.tagName == "TEXTAREA") {
if (elem.value == "") {return false;}
}
}
return true;
}
function validateAndDisplay(form,div) {
div = document.getElementById(div);
if (!validate(form)) {
div.innerHTML = "Some fields still needs to be filled.";
return false;
} else {
div.innerHTML = "";
return true;
}
}
</script>

Then in my action I have:
<FORM ACTION="../cgi-bin/CSMailto/CSMailto.cgi" METHOD=POST
enctype="multipart/form-data" onsubmit="validateAndDisplay(this,'output')">

I have these tags preceeding the form:
<div id="output"></div>

And my submit button is:
<input name="B1" type="image" src="../../../images/request_info_submit.gif">

After submitting the form with a missing required field, I am given the
screen with the message and the form (the behavior that I want), then the
screen automatically resubmits the form and goes to another page. Any ideas
why it won't stay on the screen with the message?

Thanks!
-Matt
 
L

Lee

Matt Herson said:
<FORM ACTION="../cgi-bin/CSMailto/CSMailto.cgi" METHOD=POST
enctype="multipart/form-data" onsubmit="validateAndDisplay(this,'output')">
After submitting the form with a missing required field, I am given the
screen with the message and the form (the behavior that I want), then the
screen automatically resubmits the form and goes to another page. Any ideas
why it won't stay on the screen with the message?

In order to prevent the form from submitting, the onsubmit event handler
has to return false. Yours doesn't return any value.

Note that validateAndDisplay() isn't really the onsubmit event handler.
It's simply the only line in the body of the event handler.
Change the form's onsubmit attribute to:

"return validateAndDisplay(this,'output')"
 
L

Lasse Reichstein Nielsen

Matt Herson said:
onsubmit="validateAndDisplay(this,'output')">

Should be

onsubmit="return validateAndDisplay(this,'output')">

I probably forgot that return. :)
/L
 
M

Matt Herson

Lasse Reichstein Nielsen said:
Should be

onsubmit="return validateAndDisplay(this,'output')">

I probably forgot that return. :)
/L

Thanks! I am getting closer. Now the problem seems to be after submitting
with the values missing and then getting returned to the screen again with
the form and message at the top. When I try to resubmit my values, the
submit button no longer works.
Also, it seems like even when the required values I am brought to this
screen again.

Any ideas??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top