Created: 12/16/2001
Checkbox Validator
Just a simple script to do validation for checkboxes. It validates user input to assure that there aren't too many checked, and not too few chosen.
Source code for the function at the bottom of the page.
The Source Code of the Function: (this goes in the <head> of the document)
<script type="text/javascript" language="JavaScript"> /* written by joe crawford, for fun. it's friday! 12/14/2001. */ function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion) /* This function takes 5 paramaters: whichForm -- the NAME of the form to be validated, a string whichCheckBoxArray -- the NAME of the checkbox to be checked, a string myMax -- the most you want the user to be able to check, an integer myMin -- the least you want the user to be able to check, an integer whichQuestion -- a short description of the question, a string example use: howManyChecked('myform','cb_industry',6,1,'Industry'); */ { var _countChecked = 0; var err = 0; /* iterate through all the elements in the checkbox array */ for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++) { /* and check to see if each is checked */ if(document[whichForm][whichCheckBoxArray][i].checked==true) /* if it is, increment a counter */ { _countChecked++; } } /* is the count too high? */ if(_countChecked > myMax) { alert('Limit '+myMax+' checks for the '+whichQuestion+' question.'); err = 1;} /* of is the count too low */ else if(_countChecked < myMin) { alert('You must fill out at least '+myMin+' entry(s) for the '+whichQuestion+' question.'); err = 1;} if (err == 1) { return false; } } </script>
The Source Code for invoking it: (this goes in <form> tag)
<form action="https://www.artlung.com/" name="myform" onsubmit="return howManyChecked('myform','cb_industry',6,1,'Industry');">
Source Code
<script type="text/javascript" language="JavaScript"> /* written by joe crawford for fun. it's friday! 12/14/2001. */ function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion) /* This function takes 5 paramaters: whichForm -- the NAME of the form to be validated, a string whichCheckBoxArray -- the NAME of the checkbox to be checked, a string myMax -- the most you want the user to be able to check, an integer myMin -- the least you want the user to be able to check, an integer whichQuestion -- a short description of the question, a string example use: howManyChecked('myform','cb_industry',6,1,'Industry'); */ { var _countChecked = 0; var err = 0; /* iterate through all the elements in the checkbox array */ for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++) { /* and check to see if each is checked */ if(document[whichForm][whichCheckBoxArray][i].checked==true) /* if it is, increment a counter */ { _countChecked++; } } /* is the count too high? */ if(_countChecked > myMax) { alert('Limit '+myMax+' checks for the '+whichQuestion+' question.'); err = 1;} /* of is the count too low */ else if(_countChecked < myMin) { alert('You must fill out at least '+myMin+' entry(s) for the '+whichQuestion+' question.'); err = 1;} if (err == 1) { return false; } } </script>