Ver Mensaje Individual
  #2 (permalink)  
Antiguo 05/06/2012, 20:33
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Validar que se seleccione minimo un checkbox

Tenés que recorrer todos los checkboxes con ese name, y verificar que alguno de ellos esté checked == true

Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>titulo</title>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  7. </head>
  8. <body>
  9. <form action="#" method="post" onsubmit="return verificar();">
  10. <div>
  11. <input type="checkbox" name="cap[]" value="2">
  12. <input type="checkbox" name="cap[]" value="3">
  13. <input type="checkbox" name="cap[]" value="4">
  14. <input type="checkbox" name="cap[]" value="5">
  15. <input type="submit" />
  16. </div>
  17. </form>
  18. <script type="text/javascript">
  19. //<![CDATA[
  20. function verificar(){
  21. var suma = 0;
  22. var los_cboxes = document.getElementsByName('cap[]');
  23. for (var i = 0, j = los_cboxes.length; i < j; i++) {
  24.    
  25.     if(los_cboxes[i].checked == true){
  26.     suma++;
  27.     }
  28. }
  29.  
  30. if(suma == 0){
  31. alert('debe seleccionar una casilla');
  32. return false;
  33. }else{
  34. alert(suma);
  35. }
  36.  
  37. }
  38. //]]>
  39. </script>
  40. </body>
  41. </html>

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.