Javascript check box validation

Results 1 to 3 of 3

Thread: Javascript check box validation

  1. #1
    Join Date
    Dec 1969

    Default Javascript check box validation

    Sort of a repost here, but not completely. <BR><BR>I have a dynamically created form that creates X number of checkboxes based on a call from the db, the minimum number of checkboxes that will be displayed is one, the maximum is 100.<BR><BR>The boxes are dynamically created and have a fixed naming convention, that being "box" then the number so, so "box1" "box2" "box3" etc. <BR><BR>I want to allow the users to select as many as these boxes as they like, but I want to have make sure they at least select one. So I need a validation script that will check the form on submission for an instance of at least one checkbox being checked.<BR><BR>I&#039;ve searched high and low for a script to do this but so far have been stumped, due to it not being 1 or 2 checkboxes. Can anyone help me out here?<BR><BR>Thanks in advance <BR><BR>James

  2. #2
    Join Date
    Dec 1969

    Default RE: Javascript check box validation

    Couldn&#039;t you make all of the checkboxes the same name?<BR><BR>On the server-side, you would get a comma-delimited list of what was selection. It makes it easy to split that into an array.<BR><BR>But, if you want them to have different names - add a hidden field to your form which has the # of boxes displayed.<BR><BR>Then, a simple loop will do what you need:<BR>var iCheckboxCount = document.formName.checkboxCount.value;<BR>var x = 0;<BR>var bChecked = false;<BR>while(x =&#060; iCheckboxCount && !bChecked)<BR>{<BR> bChecked = document.formName[&#039;box&#039; + x].checked;<BR> x++<BR>}<BR><BR>I think that should work.

  3. #3
    Join Date
    Dec 1969

    Default Minor fix for Netscape browsers...

    bChecked = document.formName[hl="yellow"].elements[/hl][&#039;box&#039; + x].checked;<BR><BR>Might even need that with MSIE, but for sure you do with NS.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts