Form validation problem

Discussion in 'Javascript' started by Batezz, Aug 19, 2004.

  1. Batezz

    Batezz Guest

    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="JavaScript">
    <!--
    function Blank_TextField_Validator()
    {
    if (location_search.CityTown.value="")
    { if (location_search.County.value="")
    { return (false);
    }
    }
    return (true);
    }
    -->
    </script>


    <body>
    <form action="http://www.XXXXX.COM/Product/Productsearchresults.php"
    method="get" name="location_search" id="location_search" onsubmit="return
    Blank_TextField_Validator()">
    <table width="172" border="1">
    <tr>
    <td colspan="2">Location search</td>
    </tr>
    <tr>
    <td width="64">City/Town</td>
    <td width="112"><input 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>
     
    Batezz, Aug 19, 2004
    #1
    1. Advertising

  2. Batezz

    Mick White Guest

    Batezz wrote:
    > 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="JavaScript">
    > <!--
    > function Blank_TextField_Validator()
    > {
    > if (location_search.CityTown.value="")
    > { if (location_search.County.value="")
    > { return (false);
    > }
    > }
    > return (true);
    > }
    > -->
    > </script>


    <script type="text/javascript">
    function Blank_TextField_Validator(form){
    return !(!form.CityTown.value || !form.County.value);
    }
    </script>

    <form ...
    onsubmit = "return Blank_TextField_Validator(this)">


    Mick
    > <body>
    > <form action="http://www.XXXXX.COM/Product/Productsearchresults.php"
    > method="get" name="location_search" id="location_search" onsubmit="return
    > Blank_TextField_Validator()">
    > <table width="172" border="1">
    > <tr>
    > <td colspan="2">Location search</td>
    > </tr>
    > <tr>
    > <td width="64">City/Town</td>
    > <td width="112"><input 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, Aug 20, 2004
    #2
    1. Advertising

  3. Batezz

    Lee Guest

    Batezz said:
    >
    >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="JavaScript">
    ><!--
    >function Blank_TextField_Validator()
    >{
    >if (location_search.CityTown.value="")
    >{ if (location_search.County.value="")
    >{ return (false);
    >}
    >}
    >return (true);
    >}
    >-->
    ></script>


    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.location_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.location_search.CityTown.value=="" &&
    document.location_search.County.value=="") {
    return (false);
    }
    return (true);
    }
    </script>
     
    Lee, Aug 20, 2004
    #3
  4. Batezz

    Batezz Guest

    Thank you all

    Thank's to all who helped

    Batezz
     
    Batezz, Aug 20, 2004
    #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. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    447
    Tommy
    Nov 29, 2003
  2. Sumith Mathur
    Replies:
    0
    Views:
    2,234
    Sumith Mathur
    Sep 23, 2004
  3. OriginalBrownster

    string validation/ form validation

    OriginalBrownster, Aug 21, 2006, in forum: Python
    Replies:
    1
    Views:
    441
    Simon Forman
    Aug 21, 2006
  4. Grigory Temchenko
    Replies:
    0
    Views:
    341
    Grigory Temchenko
    Dec 28, 2008
  5. bnp
    Replies:
    4
    Views:
    348
Loading...

Share This Page