Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Validating multiple text boxes with same name.


Message #1 by "Dominic Allkins" <dominic@a...> on Fri, 13 Sep 2002 10:32:58
You just get an array of them, so loop through it and check each one.  
Something like this:

function checkAreas()
{
  var bAreaEntered = false;
  for (var i=0; i<document.formname.area.length; i++)
  {
    if (document.formname.area[i].value != "")
    {
      bAreaEntered = true;
      break;
    }
  }
  if (bAreaEntered)
    alert("at least one area entered");
  else
    alert("no areas entered");
}

hth
Phil
>-----------------------------------------
> Hi All

> Hope someone can help... I have a form for entry of search criteria that 
a> llows the user to specify multiple geographic areas for the search.  
W> hen submitted, the form data is passed to an ASP page to do the search.

> Have nearly all the validation done but can't work this one out... how 
do 
I>  validate with javascript that at least one text box called 'area' has 
b> een used???  

> Am using an external script file for validating on submit and want 
t> o 'return false' if none of the area boxes have been used.

> Code for the relevant part of the form is below:

> 	<tr bgcolor="<%=ROW_MID%>">
	> 	<td width="20%" style="padding-left: 4px"><div 
c> lass="header" align="left">Areas</div></td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> </tr>
	> <tr bgcolor="<%=ROW_MID%>">
	> 	<td width="20%" style="padding: 1px">&nbsp;</td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> </tr>
	> <tr bgcolor="<%=ROW_MID%>">
	> 	<td width="20%" style="padding: 1px">&nbsp;</td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> </tr>
	> <tr bgcolor="<%=ROW_MID%>">
	> 	<td width="20%" style="padding: 1px">&nbsp;</td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> 	<td width="40%" style="padding: 1px"><input type="Text" 
n> ame="area" maxlength="20" size="20" class="textboxrequired"></td>
	> </tr>

> Hope someone can help.

> Many thanks

> Dominic

  Return to Index