Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Form validation problem

Reply
Thread Tools

Form validation problem

 
 
Batezz
Guest
Posts: n/a
 
      08-19-2004
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>


 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      08-20-2004
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>
>
>

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      08-20-2004
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>

 
Reply With Quote
 
Batezz
Guest
Posts: n/a
 
      08-20-2004
Thank's to all who helped

Batezz


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A form validation library with javascript validation Grigory Temchenko Python 0 12-28-2008 08:38 PM
string validation/ form validation OriginalBrownster Python 1 08-21-2006 01:50 AM
Struts validation for combine form fields validation Sumith Mathur Java 0 09-23-2004 06:26 PM
Form Validation Problem...Persisiting form fields on validation failure. bnp Javascript 4 05-12-2004 12:16 PM
Web form validation vs object validation Colin Basterfield ASP .Net 1 11-29-2003 12:10 AM



Advertisments