...

View Full Version : validate using ONSUBMIT vs ONUNLOAD



Tony Davis
10-12-2005, 10:18 PM
Instead of using a submit button on my form I am using an image with an onclick function. My field validation function will therefore not execute when that image/button is clicked. The form was created with GoLive, and the functions associated with the image/button are necessary, so I donít know that I can change that. So, I tried using onunload, which execute the validation function OK, but of course, if there are any errors, the next page is displayed regardless.

Does anyone have any suggestions how to get around this?
Maybe it is just a syntax issue that someone else might notice?

Here is some of the code in question:

<script type="text/javascript">
function validate_fields() {
for (x=0; x<document.pagethree.elements.length; x++) {
if (document.pagethree.elements[x].value == "") {
alert("Required fields are blank!.")
break; }
}
return false;
}
</SCRIPT>

<form onsubmit="validate_fields()">

<a title="Click to advance to next page" onclick="CSAction(new Array(/*CMP*/'62802300'));return CSClickReturn();" href="#" csclick="62802300"><img src="images/button_right_arrow.gif" alt="Click to advance to next page" name="mySubmit" height="50" width="50" border="0"></a>

When I do it like this, I get an error when I change the above to <input type="image"> that says mySubmit is null or not an objectÖ.

<input type="image" src="images/button_right_arrow.gif" title="Click to advance to next page" onclick="CSAction(new Array(/*CMP*/'62802300'));return CSClickReturn();" href="#" csclick="62802300" alt="Click to advance to next page" name="mySubmit" height="50" width="50" border="0"></div>

You can see the two examples at:

http://www.tdavisconsulting.com/kenerly/page03c.html
http://www.tdavisconsulting.com/kenerly/page03d.html

Thanks,
-tdavis

AlexVN
10-13-2005, 12:46 AM
Your validate_fields probably requires some correction:


<script type="text/javascript">
function validate_fields() {
var elements = document.pagethree.elements;
for (var x=0; x < elements.length; x++) {
if (elements[x].value == "") {
alert("Required fields are blank!.")
return false;
}
}
return true;
}
</script>


Then try the following (not sure that will work, but IMHO should):
<a title="Click to advance to next page" onclick="CSAction(new Array(/*CMP*/'62802300'));return (validate_fields() && CSClickReturn());" href="#" csclick="62802300">

felgall
10-13-2005, 12:46 AM
get rid of the name="mysubmit" out of the <input type="image" version as it is not needed.

Tony Davis
10-13-2005, 04:37 PM
I added the (validate_fields() && CSClickReturn()); to the image/button, and sure enough, the validation routine is executed. But, just like with the onunload, even if there are errors the action= program is executed and on it goes.

How do I stop it from proceeding when there are errors?
I would have thought that the return false; would do it.

Thanks,
-tdavis



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum