form validation script not working

Discussion in 'Javascript' started by Matt Herson, Sep 30, 2003.

  1. Matt Herson

    Matt Herson Guest

    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
    Matt Herson, Sep 30, 2003
    #1
    1. Advertising

  2. Matt Herson

    Lee Guest

    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')"
    Lee, Sep 30, 2003
    #2
    1. Advertising

  3. "Matt Herson" <> writes:

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


    Should be

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

    I probably forgot that return. :)
    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 1, 2003
    #3
  4. Matt Herson

    Matt Herson Guest

    "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > "Matt Herson" <> writes:
    >
    > > onsubmit="validateAndDisplay(this,'output')">

    >
    > Should be
    >
    > onsubmit="return validateAndDisplay(this,'output')">
    >
    > I probably forgot that return. :)
    > /L
    > --
    > Lasse Reichstein Nielsen -
    > 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??
    Matt Herson, Oct 1, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    1
    Views:
    3,750
    Alan Silver
    Aug 2, 2005
  2. lawrence
    Replies:
    8
    Views:
    237
    lawrence
    May 5, 2004
  3. bnp
    Replies:
    4
    Views:
    288
  4. LRW
    Replies:
    2
    Views:
    112
  5. Replies:
    5
    Views:
    172
    -Lost
    Mar 26, 2007
Loading...

Share This Page