I have a small javascript function which check validation for checkboxes:
function check_this() {
var arrCheckboxes = document.create_schedule.elements["username"];
var checkCount = 0;
for (var i = 0; i < arrCheckboxes.length; i++) {
if(arrCheckboxes[i].checked)
{ checkCount += 1;
}
}
if (checkCount > 0){
return true;
} else {
alert("You do not have any users.");
return false;
}
}
Here's the associated Perl code
print "<form action=\"create-plan.cgi\" method=\"GET\" name=\"create_schedule\" onsubmit=\"return check_this()\">";
foreach(@these_account_users)
{ my $test = $_;
my $choice =1;
foreach(@all_conf_users)
{ if ($test =~ $_)
{ #print "Duplicate";
$choice = 0;
last;
}
}
if ($choice == 1)
{ print "<input name=\"username\" value=\"$_\" type=\"checkbox\"><b>$_</b><br />";
}
else
{ print "<input name=\"username\" value=\"$_\" type=\"checkbox\" disabled=\"disabled\">$_<br />";
}
}
print '<br /><input name="backup_type" type="submit" value="Select Type">';
print "</form><br /><br />";
When this is run, it works if no checkbox is selected (i.e the alert is shown). If 2 or more check boxes exist, and atleast one is selected, it accepts it and moves to the next screen. However, if there is only one checkbox and if that is selected, it throws the alert and refuses to move forward.
What am i doing wrong?
EDIT:
On further testing, I find that if there is only one check box, arrCheckboxes.length
's value is undefined
, however if there are 2 or more accounts, arrCheckboxes.length
's value is equal to the exact number of checkboxes. Now I am really stumped.......