jennypretty
06-07-2007, 04:59 PM
Hello,
I posted a similar message about 2 weeks ago. This message is a little different than the old message.
I have a form to have checkboxes with different checkbox names because I want to store each box in different field. Some one on this forum showed me already but it worked ONLY for the same checkbox name. This time, I have different names.
I want to have a user to check at least 2 boxes and NO more than 2 boxes.
Is there any way to do that?
Thanks.
Here is my code:
function clickCtr(chk){
var ctr = 0;
var chks = chk.form.elements[chk.name];
for (var i=0; i<chks.length; i++){
if (chks[i].checked) ctr++;
}
if (ctr==2){
for (i=0; i<chks.length; i++){ //disable remaining unchecked checkboxes
if (!chks[i].checked) chks[i].disabled=true;
}
}
else{ //enable all
for (i=0; i<chks.length; i++){
chks[i].disabled=false;
}
}
}
function chFrm() {
var msgbox = "You forgot the following question (s).";
var goon = msgbox
if (document.frmName.Rank.selectedIndex==0) {
msgbox = msgbox + "\n Please select one rank";
}
var CkRadioButton = false;
for (i = 0; i < document.frmName.Gender.length; i++) {
if (document.frmName.Gender[i].checked)
CkRadioButton = true;
}
if (!CkRadioButton) {
msgbox = msgbox + "\n Please check your gender";
}
var checkboxCk = false;
for (i = 0; i < document.frmName.country.length; i++) {
if (document.frmName.country[i].checked)
checkboxCk = true;
}
if (!checkboxCk) {
msgbox = msgbox + "\n Please check your country";
}
var checkboxCk = false;
for (i = 0; i < document.frmName.Race.length; i++) {
if (document.frmName.Race[i].checked)
checkboxCk = true;
}
if (!checkboxCk) {
msgbox = msgbox + "\n Please check your race";
}
var chks = document.frmName.elements["colors"];
for (var i=0; i<chks.length; i++){
if (chks[i].checked) ctr++;
}
if (ctr<2){
msgbox = msgbox + "\n Please check 2 boxes.";
}
// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {
alert(msgbox);
return false;
}
}
Here is my form:
<input type="checkbox" name="color1" id="q" value="a" />A<BR />
<input type="checkbox" name="color2" id="q2" value="b" />b<BR />
<input type="checkbox" name="color3" id="q3" value="c" />c<BR />
<input type="checkbox" name="color4" id="q4" value="d" />d<BR />
I posted a similar message about 2 weeks ago. This message is a little different than the old message.
I have a form to have checkboxes with different checkbox names because I want to store each box in different field. Some one on this forum showed me already but it worked ONLY for the same checkbox name. This time, I have different names.
I want to have a user to check at least 2 boxes and NO more than 2 boxes.
Is there any way to do that?
Thanks.
Here is my code:
function clickCtr(chk){
var ctr = 0;
var chks = chk.form.elements[chk.name];
for (var i=0; i<chks.length; i++){
if (chks[i].checked) ctr++;
}
if (ctr==2){
for (i=0; i<chks.length; i++){ //disable remaining unchecked checkboxes
if (!chks[i].checked) chks[i].disabled=true;
}
}
else{ //enable all
for (i=0; i<chks.length; i++){
chks[i].disabled=false;
}
}
}
function chFrm() {
var msgbox = "You forgot the following question (s).";
var goon = msgbox
if (document.frmName.Rank.selectedIndex==0) {
msgbox = msgbox + "\n Please select one rank";
}
var CkRadioButton = false;
for (i = 0; i < document.frmName.Gender.length; i++) {
if (document.frmName.Gender[i].checked)
CkRadioButton = true;
}
if (!CkRadioButton) {
msgbox = msgbox + "\n Please check your gender";
}
var checkboxCk = false;
for (i = 0; i < document.frmName.country.length; i++) {
if (document.frmName.country[i].checked)
checkboxCk = true;
}
if (!checkboxCk) {
msgbox = msgbox + "\n Please check your country";
}
var checkboxCk = false;
for (i = 0; i < document.frmName.Race.length; i++) {
if (document.frmName.Race[i].checked)
checkboxCk = true;
}
if (!checkboxCk) {
msgbox = msgbox + "\n Please check your race";
}
var chks = document.frmName.elements["colors"];
for (var i=0; i<chks.length; i++){
if (chks[i].checked) ctr++;
}
if (ctr<2){
msgbox = msgbox + "\n Please check 2 boxes.";
}
// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {
alert(msgbox);
return false;
}
}
Here is my form:
<input type="checkbox" name="color1" id="q" value="a" />A<BR />
<input type="checkbox" name="color2" id="q2" value="b" />b<BR />
<input type="checkbox" name="color3" id="q3" value="c" />c<BR />
<input type="checkbox" name="color4" id="q4" value="d" />d<BR />