Forums

Checkbox validation

senox 04 May, 2009
I'm trying to make a validation, so ppl only can check >=5 things out of 30. the form is a checkbox.

i tried it with java, but as i have no clue about this language🙂

the javascript code i'm using
function chkcontrol(j) {
var total=0;
for(var i=0; i < document.ChronoContact.check0.length; i++){
if(ChronoContact_check0.check0[i].checked){
total =total +1;}
if(total > 5){
alert("Please Select only five")
document.ChronoContact.check0[j].checked = false ;
return false;
}
}
}


the form code ( a part of it)

<div class="form_item">
  <div class="form_element cf_checkbox">
    <label class="cf_label" style="display: none;">Innovation Challenge</label>
    <div class="float_left">
      <input value="Just be more innovative" class="radio radio validate-one-required" id="Just_be_more_innovative" name="check0[]" type="checkbox" onclick="chkcontrol(0)"/>
      <label for="Just_be_more_innovative" class="check_label">Just be more innovative</label>
      <br />
      
<input value="Better understand our customers needs" class="radio radio validate-one-required" id="Better_understand_our_customers_needs" name="check0[]" type="checkbox" onclick="chkcontrol(1)"/>
      <label for="Better_understand_our_customers_needs" class="check_label">Better understand our customers needs</label>
      <br />
      
<input value="Get management buy-in" class="radio radio validate-one-required" id="Get_management_buy-in" name="check0[]" type="checkbox" onclick="chkcontrol(2)"/>
      <label for="Get_management_buy-in" class="check_label">Get management buy-in</label>
      <br />
      
<input value="Balance the portfolio" class="radio radio validate-one-required" id="Balance_the_portfolio" name="check0[]" type="checkbox" onclick="chkcontrol(3)"/>
      <label for="Balance_the_portfolio" class="check_label">Balance the portfolio</label>
      <br />


hope you can help me

Senox
GreyHead 04 May, 2009
Hi Senox,

I have no idea if this will work but you do need to identify the form correctly. Check the actual form id from the form html in the browser and make sure that you use that in your code. document.ChronoContact won't work. You've also missed the document. in the if() clause.

Bob
This topic is locked and no more replies can be posted.