Form validation problem

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

    #1

    Form validation problem

    Why does the following form validation not work?

    If both CityTown and County fields are blank then the html should stop and
    not go to http://www.XXXXX.COM/Product/Productsearchresults.php.

    Any help appreciated

    Batezz

    <script Language="JavaS cript">
    <!--
    function Blank_TextField _Validator()
    {
    if (location_searc h.CityTown.valu e="")
    { if (location_searc h.County.value= "")
    { return (false);
    }
    }
    return (true);
    }
    -->
    </script>


    <body>
    <form action="http://www.XXXXX.COM/Product/Productsearchre sults.php"
    method="get" name="location_ search" id="location_se arch" onsubmit="retur n
    Blank_TextField _Validator()">
    <table width="172" border="1">
    <tr>
    <td colspan="2">Loc ation search</td>
    </tr>
    <tr>
    <td width="64">City/Town</td>
    <td width="112"><in put name="CityTown" type="text" id="CityTown"
    size="15" /></td>
    </tr>
    <tr>
    <td>County</td>
    <td><input name="County" type="text" id="County" size="15" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><div align="right">
    <input type="submit" name="Submit" value="Search" />
    </div></td>
    </tr>
    </table>
    </form>
    </body>


  • Mick White

    #2
    Re: Form validation problem

    Batezz wrote:[color=blue]
    > Why does the following form validation not work?
    >
    > If both CityTown and County fields are blank then the html should stop and
    > not go to http://www.XXXXX.COM/Product/Productsearchresults.php.
    >
    > Any help appreciated
    >
    > Batezz
    >
    > <script Language="JavaS cript">
    > <!--
    > function Blank_TextField _Validator()
    > {
    > if (location_searc h.CityTown.valu e="")
    > { if (location_searc h.County.value= "")
    > { return (false);
    > }
    > }
    > return (true);
    > }
    > -->
    > </script>[/color]

    <script type="text/javascript">
    function Blank_TextField _Validator(form ){
    return !(!form.CityTow n.value || !form.County.va lue);
    }
    </script>

    <form ...
    onsubmit = "return Blank_TextField _Validator(this )">


    Mick[color=blue]
    > <body>
    > <form action="http://www.XXXXX.COM/Product/Productsearchre sults.php"
    > method="get" name="location_ search" id="location_se arch" onsubmit="retur n
    > Blank_TextField _Validator()">
    > <table width="172" border="1">
    > <tr>
    > <td colspan="2">Loc ation search</td>
    > </tr>
    > <tr>
    > <td width="64">City/Town</td>
    > <td width="112"><in put name="CityTown" type="text" id="CityTown"
    > size="15" /></td>
    > </tr>
    > <tr>
    > <td>County</td>
    > <td><input name="County" type="text" id="County" size="15" /></td>
    > </tr>
    > <tr>
    > <td>&nbsp;</td>
    > <td><div align="right">
    > <input type="submit" name="Submit" value="Search" />
    > </div></td>
    > </tr>
    > </table>
    > </form>
    > </body>
    >
    >[/color]

    Comment

    • Lee

      #3
      Re: Form validation problem

      Batezz said:[color=blue]
      >
      >Why does the following form validation not work?
      >
      >If both CityTown and County fields are blank then the html should stop and
      >not go to http://www.XXXXX.COM/Product/Productsearchresults.php.
      >
      >Any help appreciated
      >
      >Batezz
      >
      ><script Language="JavaS cript">
      ><!--
      >function Blank_TextField _Validator()
      >{
      >if (location_searc h.CityTown.valu e="")
      >{ if (location_searc h.County.value= "")
      >{ return (false);
      >}
      >}
      >return (true);
      >}
      >-->
      ></script>[/color]

      There are several problems with that code.
      The <script> tag should have a type attribute.
      You should not use the <!-- and --> comments.
      You should refer to the form as document.locati on_search
      (or better, pass a reference to the form to the function).
      The equality test operator is "==", not "=".
      You should use indentation to make it easier to read.
      The logic could be simplified, and it should also be made
      to test for nothing but whitespace, but without going that
      far, your code should look like:

      <script type="text/javascript">
      function Blank_TextField _Validator() {
      if (document.locat ion_search.City Town.value=="" &&
      document.locati on_search.Count y.value=="") {
      return (false);
      }
      return (true);
      }
      </script>

      Comment

      • Batezz

        #4
        Thank you all

        Thank's to all who helped

        Batezz


        Comment

        Working...