Foros del Web » Programando para Internet » Javascript »

Validar un checkbox

Estas en el tema de Validar un checkbox en el foro de Javascript en Foros del Web. Hola amigos! las estoy pasando realmente canutas queriendo hacer obligatorio el rellandao de unos checkbox y no lo consigo, he probado mil cosas y nada ...
  #1 (permalink)  
Antiguo 27/04/2012, 05:10
 
Fecha de Ingreso: enero-2012
Mensajes: 4
Antigüedad: 12 años, 2 meses
Puntos: 0
Exclamación Validar un checkbox

Hola amigos!
las estoy pasando realmente canutas queriendo hacer obligatorio el rellandao de unos checkbox y no lo consigo, he probado mil cosas y nada

os dejo el codigo del formulario entero, los check box estan al final del todo.
si pudierais dejarme un codigo JS que funcione.

Gracias de antemano!

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WKF - Formulaire commande catalogue formation</title>

<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0

if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_valida teForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+=' Vous devez bien ecrire une direction e-mail.\n';
} else if (test!='B') { num = parseFloat(val);

if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '\n'; }
} if (errors) alert('Vous devez remplir les champs obligatoires\n'+errors);
document.MM_returnValue = (errors == '');

} }
function MM_showHideLayers() { //v9.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3)
with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
obj.visibility=v; }
}
</script>




</head>

<body>
<div id="top_form" style=" width:800px; height:auto; margin: 0px auto auto; margin-bottom:20px; border-bottom: solid 3px #F00; background-color:#FFF ">
<div style="width:800px; border-bottom: solid #F00; background-color:#FFF; margin-top: 12px;">
<img src="../images/autre/logoLSform.jpg" />

</div>

<div class="Estilo2" style="text-align:left; font-family: Arial, Helvetica, sans-serif ; color:black; font-size:0.7em; padding-left: 80px;">

<form id="form1" name="form1" method="post" action="sendLS.php" onsubmit="return performCheck('form1', rules, 'inline');return false;">
<h3 style="color:#F00; margin: 20px -57px;">Pour télécharger le catalogue 2012, merci de renseigner le formulaire ci-dessous :</h3>
<p>

<label>
<input type="radio" name="civilite" value="madame" id="civ_0" />
Madame</label>

<label>
<input type="radio" name="civilite" value="madmoiselle" id="civ_0" />
Mademoiselle</label>

<label>
<input type="radio" name="civilite" value="monsieur" id="civ_0" />
Monsieur</label>
</p>

<p>
Prénom<cite style="color:red">*</cite><br />
<input name="prenom" type="text" id="prenom" style="width:200px" />
</span><br />
Nom<cite style="color:red">*</cite><br />
<label>
<input name="nombrele" type="text" id="nombre" style="width:200px" />
</label>
<br />

Email<cite style="color:red">*</cite><br />
<input name="email" type="text" id="email" style="width:200px" />
<br />
Fonction<cite style="color:red">*</cite><br />
<input name="fonction" type="text" id="fonction" style="width:200px" /><br />
Raison sociale<cite style="color:red">*</cite><br />
<input name="rsociale" type="text" id="rsociale" style="width:200px" />
</p>
<p style="color:red; font-size:0.9em; margin-top:-5px">Les champs en rouge sont obligatoires</p>

<h3 style="color:#F00; margin: 20px -57px;">Centre(s) d´intérêt<cite style="color:red">*</cite></h3>

<p>
<label>
<input name="interet" type="checkbox" id="interet" value=" 1 " />
Pratique du droit social</label>
<br />
<label>
<input type="checkbox" name="interet0" value=" 2 " id="interet_0" />
Relations sociales</label>
<br />
<label>
<input type="checkbox" name="interet1" value=" 3 " id="interet_1" />
Stratégie et pilotage des RH</label>
<br />
<label>
<input type="checkbox" name="interet2" value="4 " id="interet_2" />
Formation et développement des RH</label>
<br />
<label>
<input type="checkbox" name="interet3" value="5 " id="interet_3" />
Santé au travail</label>
<br />
<label>
<input type="checkbox" name="interet4" value="6 " id="interet_4" />
Paye, rémunérations et protection sociale</label>
<br />
<label>
<input type="checkbox" name="interet5" value="Représentants Personnel " id="interet_5" />
Représentants du personnel</label>
<br />

</p>
<p>
<label>
<input style="margin-bottom:40px; margin-top:40px" name="Submit" type="submit" onclick="MM_validateForm('prenom','','R','nombre', '','R','email','','RisEmail', 'sisi','' );return document.MM_returnValue" value="Envoyer" />
</label>
</p>

</form>

</div>
</div>
</body>
</html>
  #2 (permalink)  
Antiguo 27/04/2012, 05:19
 
Fecha de Ingreso: enero-2012
Mensajes: 4
Antigüedad: 12 años, 2 meses
Puntos: 0
Respuesta: Validar un checkbox

el ultimo function no hace nada, n osirve para nada!!!!
como dato, que se me olvido quitarlo del codigo
  #3 (permalink)  
Antiguo 30/04/2012, 11:34
Avatar de diamari  
Fecha de Ingreso: marzo-2012
Mensajes: 95
Antigüedad: 12 años
Puntos: 30
Respuesta: Validar un checkbox

Ahora debería funcionar:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WKF - Formulaire commande catalogue formation</title>

<script type="text/javascript">

function MM_validateForm() { //v4.0

if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_valida teForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if(val != null && test=='Rchecked'){ var z = ''; var marcado = false;
while(document.getElementById(nm + z)){ var elemento = document.getElementById(nm + z);
if(elemento.checked) marcado = true; if(z == ''){ z = 1;}else{ z++;}}
if(marcado==false){ val.value = '';}}

if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+=' Vous devez bien ecrire une direction e-mail.\n';
} else if (test!='B') { num = parseFloat(val);

if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R')errors += '\n';}
} if (errors) alert('Vous devez remplir les champs obligatoires\n'+errors);
document.MM_returnValue = (errors == '');

} }
</script>




</head>

<body>
<div id="top_form" style=" width:800px; height:auto; margin: 0px auto auto; margin-bottom:20px; border-bottom: solid 3px #F00; background-color:#FFF ">
<div style="width:800px; border-bottom: solid #F00; background-color:#FFF; margin-top: 12px;">
<img src="http://www.forosdelweb.com/f13/images/autre/logoLSform.jpg" />

</div>

<div class="Estilo2" style="text-align:left; font-family: Arial, Helvetica, sans-serif ; color:black; font-size:0.7em; padding-left: 80px;">

<form id="form1" name="form1" method="post" action="sendLS.php" onsubmit="return performCheck('form1', rules, 'inline');return false;">
<h3 style="color:#F00; margin: 20px -57px;">Pour télécharger le catalogue 2012, merci de renseigner le formulaire ci-dessous :</h3>
<p>

<label>
<input type="radio" name="civilite" value="madame" id="civ_0" />
Madame</label>

<label>
<input type="radio" name="civilite" value="madmoiselle" id="civ_0" />
Mademoiselle</label>

<label>
<input type="radio" name="civilite" value="monsieur" id="civ_0" />
Monsieur</label>
</p>

<p>
Prénom<cite style="color:red">*</cite><br />
<input name="prenom" type="text" id="prenom" style="width:200px" />
</span><br />
Nom<cite style="color:red">*</cite><br />
<label>
<input name="nombrele" type="text" id="nombre" style="width:200px" />
</label>
<br />

Email<cite style="color:red">*</cite><br />
<input name="email" type="text" id="email" style="width:200px" />
<br />
Fonction<cite style="color:red">*</cite><br />
<input name="fonction" type="text" id="fonction" style="width:200px" /><br />
Raison sociale<cite style="color:red">*</cite><br />
<input name="rsociale" type="text" id="rsociale" style="width:200px" />
</p>
<p style="color:red; font-size:0.9em; margin-top:-5px">Les champs en rouge sont obligatoires</p>

<h3 style="color:#F00; margin: 20px -57px;">Centre(s) d´intérêt<cite style="color:red">*</cite></h3>

<p>
<label>
<input name="interet" type="checkbox" id="interet" value=" 1 " />
Pratique du droit social</label>
<br />
<label>
<input type="checkbox" name="interet0" value=" 2 " id="interet1" />
Relations sociales</label>
<br />
<label>
<input type="checkbox" name="interet1" value=" 3 " id="interet2" />
Stratégie et pilotage des RH</label>
<br />
<label>
<input type="checkbox" name="interet2" value="4 " id="interet3" />
Formation et développement des RH</label>
<br />
<label>
<input type="checkbox" name="interet3" value="5 " id="interet4" />
Santé au travail</label>
<br />
<label>
<input type="checkbox" name="interet4" value="6 " id="interet5" />
Paye, rémunérations et protection sociale</label>
<br />
<label>
<input type="checkbox" name="interet5" value="Représentants Personnel " id="interet6" />
Représentants du personnel</label>
<br />

</p>
<p>
<label>
<input style="margin-bottom:40px; margin-top:40px" name="Submit" type="submit" onclick="MM_validateForm('interet','','Rchecked',' prenom','','R','nombre','','R','email','','RisEmai l','sisi','' );return document.MM_returnValue" value="Envoyer" />
</label>
</p>

</form>

</div>
</div>
</body>
</html>

solo tienes que añadir el id de los check y Rchecked, los checkbox necesitan es enumerados de la siguiente forma:

check
check1
check2
check3
etc...

Etiquetas: checkbox, obligatorio
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 18:05.