• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 783
  • Last Modified:

Javascript checkbox validation

Hi,
I am getting problem with javascript checkbox validation...I would appreciate if anybody help on this... I am using this tag, to display list of records with checkboxes, if none is checked I need to throw an alert ("check atleast one").

 <display:column media="html" style="width: 90px;text-align: center;" title="Select All&nbsp;<input type='checkbox' name='all' onclick='checkall(this.form.updateList)'>" > <html:multibox property="updateList" title="Select" ><c:out value="${uid_rowNum}"/> </html:multibox> </display:column>

I did something like this...updateList is an array...it says undefined...

function validate() {
      var blnFlag = 0;
      for (cnt = 0; cnt <updateList.length; cnt++)
      {
            if (document.forms[0].updateList[cnt].checked)
            {
            blnFlag = 1;
            }
      }

      if (blnFlag == 0){
      alert("Please select at least one record.");
      }

}//end function
0
prreddy
Asked:
prreddy
1 Solution
 
mrichmonCommented:
Well in the html source each checkbox should have a name right?

Here is general code to see if at least one is checked:

function checkCheckBoxes()
{
      if (document.myform.checkbox1.checked == false && document.myform.checkbox2.checked == false && document.myform.checkbox3.checked == false)
      {
            alert ('Check at least one!');
            return false;
      }
        else
        {       
            return true;
      }
}

<form name="myform" onSubmit="return checkCheckBoxes();" action="yourpage" method="post">
<input type="Checkbox" name="checkbox1">Pick Me - I'm #1
<input type="Checkbox" name="checkbox2">Pick Me - I'm #2
<input type="Checkbox" name="checkbox3">Pick Me - I'm #3
</form>
0
 
prreddyAuthor Commented:
Actually its an array... Can you see what's wrong in this script....everytime its going to else clause...

function validate() {

      var blnFlag = 0;

      var cnt=0;

      var updateList = '<%=updList%>';

      alert("this is in validate.>>>>>updateList>>>>"+updateList.length);

      alert("this is in validate"+document.forms[0].updateList[cnt].checked);

     
      if (document.forms[0].updateList[cnt].checked==true)

            {

                  alert("P@@@@@@@@@@@@@@@@@@@@@@@@@@@2.");

                  return true;

            }else{

                  alert("Please select at least one record to update.");

                  return false;

      }

           

 

}//end function
0
 
labrynth82Commented:
if (document.forms[0].updateList[cnt].checked=1) {
      alert("P@@@@@@@@@@@@@@@@@@@@@@@@@@@2.");
      return true;
}
else {      
      alert("Please select at least one record to update.");
      return false;
}

changed "true" to 1.

Value for checkbox is 1 = checked, 0 = not checked.
0
 
pratcpCommented:
The problem is when you are trying to reference updateList in the for loop.

function validate() {
     var blnFlag = 0;
     for (cnt = 0; cnt <document.forms[0].updateList.length; cnt++) <------------------------------
     {
          if (document.forms[0].updateList[cnt].checked)
          {
          blnFlag = 1;
          }
     }

     if (blnFlag == 0){
     alert("Please select at least one record.");
     }

}//end function
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now