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

form validation script not working

P: n/a
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[i];
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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')"

Jul 20 '05 #2

P: n/a
"Matt Herson" <eb***@mherson.com> writes:
onsubmit="validateAndDisplay(this,'output')">


Should be

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

I probably forgot that return. :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:n0**********@hotpop.com...
"Matt Herson" <eb***@mherson.com> writes:
onsubmit="validateAndDisplay(this,'output')">


Should be

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

I probably forgot that return. :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


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??
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.