Foros del Web » Programando para Internet » Javascript »

validar checkbox

Estas en el tema de validar checkbox en el foro de Javascript en Foros del Web. Hola gentes les comento hice una consulta para borrar varios mensajes con asp mediante checkbox pero quiero validarlos y tengo el siguiente codigo pero el ...
  #1 (permalink)  
Antiguo 05/05/2002, 23:31
Avatar de dieguicho  
Fecha de Ingreso: noviembre-2001
Ubicación: Buenos Aires
Mensajes: 1.190
Antigüedad: 22 años, 5 meses
Puntos: 1
validar checkbox

Hola gentes les comento
hice una consulta para borrar varios mensajes con asp mediante checkbox pero quiero validarlos y tengo el siguiente codigo
pero el problema es que si no checkeo dos como minimo me da el mensaje "No hay mensajes seleccionados" la idea es que al checkear minimo 1 checkbox ya acepte (todos los checkbox se llaman Id)
Ademas me gustaria agregarle un complemento de que si estan checkeados 1 o 50 checkboxes me pida una confirmacion onda "desea borrar los mensajes seleccionados" ACEPTAR - CANCELAR
pero se poco y nada de javascript y hasta aca llegue.
este es el codigo

<script language="JavaScript">
<!--
function Del_Validator(theForm)
{

var checkSelected = false;
for (i = 0; i < theForm.Id.length; i++)
{
if (theForm.Id.checked)
checkSelected = true;
}
if (!checkSelected)
{
alert("No hay mensajes seleccionados para borrar.");
return (false);
}
}
//-->
</script>
<form action="borrar.asp" name="Del" method="post" onsubmit="return Del_Validator(this)">
<input type="checkbox" name="Id" value="1">
<input type="checkbox" name="Id" value="2">
<input type="checkbox" name="Id" value="3">
</form>
  #2 (permalink)  
Antiguo 09/05/2002, 08:41
bet
 
Fecha de Ingreso: febrero-2001
Mensajes: 292
Antigüedad: 23 años, 2 meses
Puntos: 0
Re: validar checkbox

Si no entendí mal, esto es lo que estás necesitando...
Código:
<html>
<head>
<title> Uno solo </title>

<script language="JavaScript">
<!--
function checkForm(frm){
	var x = 0, z = 0;
	for (i=0;i<document.forms[0].length;i++){
		if ((document.forms[0].elements[ i].type) == "checkbox"){
			z = (document.forms[0].elements[ i].checked)?1:0;
			x = x + z;
		}
	}
	if (x < 1){
		alert("No marcó ninguno");
		return false;
	}
	else{
			if(x==1){
				document.forms[0].submit();
			}
			else {
				n= confirm("Quiere desmarcar lo seleccionado");
				if(n){
					desmarcarTodos();
					return false;
				}
			}
		}
}


function desmarcarTodos(){
		for (i=0;i<document.forms[0].length;i++){
			if ((document.forms[0].elements[ i].type) == "checkbox"){
				document.forms[0].elements[ i].checked = false;
			}
		}
}

//-->
</script>
</head>

<body bgcolor="#FFFFFF">
<form method="post" action="" onsubmit="return checkForm(this);">
Marcar:<br>
<input type="checkbox"> <br>
<input type="checkbox"> <br>
<input type="checkbox"> <br>
<input type="checkbox"> <br>
<input type="checkbox"> <br>
<input type="submit">
</form>

</body>
</html>
<hr noshade size=1><img src="http://www.gograph.com/Images-8712/ClipArt/cat03.gif" height="50" border=0 align="absmiddle"> <font size="2" face="verdana" color="#000000">bet[/CODE]
  #3 (permalink)  
Antiguo 09/05/2002, 14:53
Avatar de dieguicho  
Fecha de Ingreso: noviembre-2001
Ubicación: Buenos Aires
Mensajes: 1.190
Antigüedad: 22 años, 5 meses
Puntos: 1
Re: validar checkbox

perfect!!
si pero la idea era si hay 1 o mas checkeados que aparezca la confirmacion onda desea enviar el formulario??
si hace aceptar se envie y si cancelar que qeude todo como esta
estoy toqueteando a ver si lo peudo hacer cho
muuchas gracias
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:12.