Form validation problem

B

Batezz

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

Mick White

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>

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

Lee

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top