Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2005, 09:18 PM   PM User | #1
Tony Davis
New Coder

 
Join Date: Aug 2003
Location: Atlanta, Georgia
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Tony Davis is an unknown quantity at this point
validate using ONSUBMIT vs ONUNLOAD

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
Tony Davis is offline   Reply With Quote
Old 10-12-2005, 11:46 PM   PM User | #2
AlexVN
New Coder

 
Join Date: Oct 2005
Location: Simferopol, Crimea, Ukraine
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
AlexVN is an unknown quantity at this point
Your validate_fields probably requires some correction:
Code:
<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">
__________________
Alexander
AlexVN is offline   Reply With Quote
Old 10-12-2005, 11:46 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
get rid of the name="mysubmit" out of the <input type="image" version as it is not needed.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 10-13-2005, 03:37 PM   PM User | #4
Tony Davis
New Coder

 
Join Date: Aug 2003
Location: Atlanta, Georgia
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Tony Davis is an unknown quantity at this point
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
Tony Davis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:13 PM.


Advertisement
Log in to turn off these ads.