Hi all,
I am trying to validate search fields in a search form. I want to make sure both fields are filled in before the form is allowed to be submitted.
I have started with the following code to get the first field validated:
<script type="javascript">
function validateForm()
{
var x=document.forms["custom-search"]["ls"].value;
if (x==null || x=="")
{
alert("Please enter something to search for");
return false;
}
}
</script>
But when I submit the form, it doesn’t even validate that field. So before I even go ahead and attempt to validate both fields - I am wondering why the validation isn’t working.
This is the form:
<form NAME="custom-search" onsubmit="return validateForm()" method="get" action="http://cbresources.co.uk">
<label for="search-text">
<span class="search-title">Search For </span><span class="search-help">(e.g. restaurant, web designer, florist)</span>
</label>
<div class="input-cont h39">
<div class="left h39"></div>
<div class="mid h39">
<input type="text" name="ls" id="search-text" class="text" value="" />
</div>
<div class="right h39"></div>
</div>
</div>
<div class="search-location">
<label for="search-location">
<span class="search-title">Near </span><span class="search-help">(city, country)</span>
</label>
<div class="input-cont h39">
<div class="left h39"></div>
<div class="mid h39">
<input type="text" name="location" id="search-location" class="text" value="" />
</div>
<div class="right h39"></div>
</div>
</div>
<div class="search-button">
<!-- <input type="image" src="http://cbresources.co.uk/wp-content/themes/vantage/images/search.png" value="Search" /> -->
<button type="submit" id="search-submit" class="rounded-small">Search</button>
</div>
</div>
</form>
Should I be doing something different because the “Method” is GET and not POST or because the Action directs it back to the same page instead of an external search script?
This is quite an urgent request so anybody who is available to help me figure this out tonight would be a hero!
Also, like I said before, I need to be able to validate BOTH fields - I want to make sure both “ls” and “location” are filled in before the user is allowed to search.
The test page can be viewed at http://cbresources.co.uk
Regards,
Chris