free hosting   image hosting   hosting reseller   online album   e-shop   famous people 
Free Website Templates
Free Installer

Checkbox Validator


Unlike radio buttons, checkboxes do not cancel each other out.

But if you only want one checkbox to be selected at any time, this script will do it.


Paste the code below into the head section of your page:
Now paste this code in the body of your page:

How It Works

In this example, there are 8 checkboxes.

In the body section, each is given an index, from 0-7.


When any box is checked, the function checkBoxValidate() is called:
Item 1<input type="checkbox" name="ckbox"
onClick="javascript:checkBoxValidate(0)">
Above, Item1 passes the index parameter 0.


Using a simple counter, the function clears any box that is already checked:
for (j = 0; j < 8; j++)
if (eval("document.myform.ckbox[" + j + "].checked") == true)
document.myform.ckbox[j].checked = false;
Next, if the counter j is equal to the index parameter cb, the corresponding box is checked:
if (j == cb)
document.myform.ckbox[j].checked = true;

Try It Out


Item 1 Item 2 Item 3 Item 4

Item 5 Item 6 Item 7 Item 8





© 2001 CyberSchool