Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Oct 2005
    Location
    London, United Kingdom
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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">
    Code Review 4U - boost your code and skills for
    being social - JavsScript and C# code reviews.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •