473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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="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>
Jul 23 '05 #1
3 1388
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>

Jul 23 '05 #2
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>

Jul 23 '05 #3
Thank's to all who helped

Batezz
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
6
by: Darren | last post by:
I have a form that has 10 fields on it. I have made all of them "Required". I also am using vb if statements to decide whether or not each field should be on the page. I am using the vb to...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
4
by: Greg Scharlemann | last post by:
I'm trying to setup a dyamic dropdown list that displays a number of text fields based on the selected number in the dropdown. The problem I am running into is capturing the data already entered...
10
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.