Click to See Complete Forum and Search --> : Help with validation?


jlmaz
01-20-2003, 09:35 PM
I've been stuck on this for days, and am hoping someone can help. On our webpage, there is a search box which is an input type="text" box. Due to some fatal errors on the page that happen if someone does NOT put in a keyword to search for, I want to make it impossible for the search to take place unless there is a keyword and instead popup an alert. I need for this to work both if they click on the "go" image and if they hit enter. There are also other forms on the page, so I can't just disable the enter key entirely. I tried the following code to at least try to have it work if they click on the "go" image, but that's not even working right :(

If anyone could help I'd really appreciate it.


{literal}
<script language="javascript">
<!--
function Validate()
{
for (i=0;i<document.forms[0].elements.length;i++)
{
if (document.forms[0].elements[i].value=="")
{
alert("Please enter a keyword in the search box ");
document.forms[0].elements[i].focus();
return false ;
}
else {
document.productsearchform.submit();
}
}
}
//-->
</script>
{/literal}


then around the go image:


<form method="get" action="search.php" name=productsearchform>
<tr>
<td valign="middle" class="WhiteLine" width=70>
<p>
<span class="TableTopLabel">
{$lng.lbl_search}:
</span></p>
</td>
<td valign="middle" class="WhiteLine">
<input type="text" name="substring" size="12"
value="{$smarty.get.substring|escape}">
<span class="TableCenterProductPriceBlue">
<a onclick="java script:Validate()" href="#">
<img src="../{ #ImagesDir# }
/GOboutonWhiteBkg.gif" width=28 height=13
border=0 align=top></a>
</span>
</td>
</tr>


Any suggestions?

vickers_bits
01-20-2003, 09:53 PM
for the actual validation you're using forms[0] but when you submit it you use the name.
could it be that the search form is not the 1st on the page?

and you would benefit from using the onsubmit event handler in the <form> tag

function Validate(){
frm=document.forms["productsearchform"];
for (x=0;x<frm.length;x++){
if (frm.elements[x].type=="text" && frm.elements[x].value==""){
alert("All Fields Required");
return false;
}
}
};

jlmaz
01-20-2003, 11:13 PM
Thanks so much for responding :)

I tried the code you suggested, and if I just replace what I had with yours, the alert comes up fine, but I can't actually search for any images with the GO image. (The browser just sort of flashes briefly and then sits there when go is clicked.)

If I put in what you suggested and replace the form part with:


<form method="get" action="search.php"
onsubmit="javascript:Validate()" name=productsearchform>
<tr>
<td valign="middle" class="WhiteLine" width=70>
<p>
<span class="TableTopLabel">
&nbsp;{$lng.lbl_search}:
</span>
</p>
</td>
<td valign="middle" class="WhiteLine">
<input type="text" name="substring" size="12"
value="{$smarty.get.substring|escape}">
<span class="TableCenterProductPriceBlue">
<a href="javascript:document.productsearchform.submit()">
{include file="buttons/search_head.tpl"}</a>
</span>
</td>
</tr>


then I can search for things but I no longer get the alert and I'm back to the fatal errors as well. Did I mess something up in the form portion this time?