form validation script not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Herson

    form validation script not working

    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.l ength; 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 validateAndDisp lay(form,div) {
    div = document.getEle mentById(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="multip art/form-data" onsubmit="valid ateAndDisplay(t his,'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_su bmit.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


  • Lee

    #2
    Re: form validation script not working

    Matt Herson said:
    [color=blue]
    ><FORM ACTION="../cgi-bin/CSMailto/CSMailto.cgi" METHOD=POST
    >enctype="multi part/form-data" onsubmit="valid ateAndDisplay(t his,'output')">[/color]
    [color=blue]
    >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?[/color]

    In order to prevent the form from submitting, the onsubmit event handler
    has to return false. Yours doesn't return any value.

    Note that validateAndDisp lay() 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 validateAndDisp lay(this,'outpu t')"

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: form validation script not working

      "Matt Herson" <ebay2@mherson. com> writes:
      [color=blue]
      > onsubmit="valid ateAndDisplay(t his,'output')">[/color]

      Should be

      onsubmit="retur n validateAndDisp lay(this,'outpu t')">

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

      Comment

      • Matt Herson

        #4
        Re: form validation script not working


        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:n0clc0zn.f sf@hotpop.com.. .[color=blue]
        > "Matt Herson" <ebay2@mherson. com> writes:
        >[color=green]
        > > onsubmit="valid ateAndDisplay(t his,'output')">[/color]
        >
        > Should be
        >
        > onsubmit="retur n validateAndDisp lay(this,'outpu t')">
        >
        > I probably forgot that return. :)
        > /L
        > --
        > Lasse Reichstein Nielsen - lrn@hotpop.com
        > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        > 'Faith without judgement merely degrades the spirit divine.'[/color]

        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??


        Comment

        Working...