Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to validate an array of checkboxes named in ASP?

Status
Not open for further replies.

Rachel80

Programmer
May 25, 2000
63
GB
Hi,

i have an array of 19 checkboxes as follows:

<% dim temp_id(19)

for i=0 to 18
temp_id(i) = &quot;id&quot; & i
next

for j=0 to 18 %>
<input type=&quot;text&quot; name=&quot;<%=temp_id(j)%>&quot; size=15 maxlength=&quot;20&quot;>
<% next %>

As shown, each checkbox has a unqiue name (eg. id1, id2, ..)
Thus I wonder in JS, how can i validate all the checkboxes?
say,

if (form.id1.value.length == 0)
{
alert(&quot;Empty field not allowed&quot;);
form.id1.focus();
return false
}

I do not want to repeat this 19 times as i want to use arrays.. However, the form name for the checkboxes are in
<%=temp_id(j)%> this format, how can i do the validation? Pls help, thanx!
 
Here's how you can validate input type=&quot;text&quot; (untested - from memory):
Code:
    <input type=&quot;text&quot; name=&quot;<%=temp_id(j)%>&quot; size=15

function chkMe() {
  var howmany = 19;
  for (i = 1; i < howmany; i++) {
    fld = eval('document.forms[0].id' + i);
    if (fld.value.length == 0) {
      alert(&quot;Empty field not allowed&quot;);
      fld.focus();
      return false;
    }
  }
}

 
oh thanx so much, your code is great! :-D
Very helpful to me, thanx again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top