topical media & game development

talk show tell print

basic-web-html-12-ch12-eg08.htm / htm



  <html>
  <head>
    <title>Form validation</title>
    <script type="text/JavaScript" language="JavaScript">
  
  function countCheckboxes(field) {
    var intCount = 0
    for (var i = 0; i < field.length; i++) {
               if (field[i].checked)
           intCount++; }
    alert("You selected " + intCount + " checkbox(es)");
  }
  
  </script>
  </head>
  <body>
  
  <form name="frmCards" action="cards.aspx" method="post">
    <p>Please select a suit of cards.</p>
    <p><input type="checkbox" name="chkSuit" value="hearts" /> Hearts </p>
    <p><input type="checkbox" name="chkSuit" value="diamonds" /> Diamonds </p>
    <p><input type="checkbox" name="chkSuit" value="spades" /> Spades </p>
    <p><input type="checkbox" name="chkSuit" value="clubs" /> Clubs </p>
    <p><input type="button" value="Count checkboxes" onclick="countCheckboxes(frmCards.chkSuit)" /></p>
  </form> 
  
  </body>
  </html>


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.