Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 JavaScript: Count checkboxes Jump to:  
Category: >> JavaScript >> Count checkboxes Bookmark and Share

<< lastnext >>

Snippet Name: Count checkboxes

Description: Make sure users can only select X number of checkboxes.

Comment: (none)

Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script>
FUNCTION check(){
count = 0;
str = '';
    FOR(x=0; x<document.form1.elements["checkbox[]"].length; x++){
        IF(document.form1.elements["checkbox[]"][x].checked==TRUE){
            str += document.form1.elements["checkbox[]"][x].value + ',';
            count++;
        }
    }
 
    IF(count==0){
        ALERT("You must choose at least 1");
        RETURN FALSE;
    }
    ELSE IF(count>3){
        ALERT("You can choose a maximum of 3");
        RETURN FALSE;
    }
    ELSE {
    ALERT("You chose " + count + ": " + str.substring(0,str.length-1));
    document.form1.submit();
    }
}
</script>
</head>
 
<body>
 
<form NAME="form1" id="form1" method="post" action="" onsubmit="return check()">
  <p>
    <input type="checkbox" NAME="checkbox[]" value="red" />
  Red<br />
  <input type="checkbox" NAME="checkbox[]" value="green" />
  Green<br />
  <input type="checkbox" NAME="checkbox[]" value="blue" />
  Blue<br />
  <input type="checkbox" NAME="checkbox[]" value="black" />
  Black<br />
  <input type="checkbox" NAME="checkbox[]" value="white" />
  White<br />
  <input type="checkbox" NAME="checkbox[]" value="yellow" />
  Yellow<br />
  <input type="checkbox" NAME="checkbox[]" value="purple" />
  Purple
</p>
  <p>
    <input type="submit" NAME="Submit" value="Click Me" />
</p>
</form>
</body>
</html> 


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 179 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?