Foros del Web » Programando para Internet » Javascript »

Problema en un formulario

Estas en el tema de Problema en un formulario en el foro de Javascript en Foros del Web. Todo me va bien salvo el ultimo alert (observaciones) que no me funciona no tengo ni idea del porque: <script Language="JavaScript" Type="text/javascript"><!-- function FrontPage_Form1_Validator(theForm) { ...
  #1 (permalink)  
Antiguo 01/03/2006, 09:03
Avatar de niconico  
Fecha de Ingreso: enero-2006
Mensajes: 166
Antigüedad: 18 años, 4 meses
Puntos: 0
Problema en un formulario

Todo me va bien salvo el ultimo alert (observaciones) que no me funciona no tengo ni idea del porque:


<script Language="JavaScript" Type="text/javascript"><!--
function FrontPage_Form1_Validator(theForm)
{

if (theForm.hito.value == "")
{
alert("Escriba un valor para el campo Hito.");
theForm.hito.focus();
return (false);}

if (theForm.tareas.value == "")
{
alert("Escriba un valor para el campo Tareas.");
theForm.tareas.focus();
return (false);}

if (theForm.inicio.value.length < 5)
{
alert("Escriba un valor correcto para el campo Inicio");
theForm.inicio.focus();
return (false);}
if (theForm.observaciones.value == "")
{
alert("Escriba un valor para el campo Observaciones.");
theForm.observaciones.focus();
return (false);}
return (true);
}
</script>

El input de observaciones es :
<INPUT NAME="observaciones" TYPE="TEXT" class="Estilo21">
Estoy cansado de mirarlo y no veo el problema todos me funcionan salvo el campo observaciones, muchas gracias!!
  #2 (permalink)  
Antiguo 01/03/2006, 12:09
Avatar de chalchis  
Fecha de Ingreso: julio-2003
Mensajes: 1.773
Antigüedad: 20 años, 9 meses
Puntos: 21
Pregunta no sera que le falta una else

if (theForm.observaciones.value == "")
{
alert("Escriba un valor para el campo Observaciones.");
theForm.observaciones.focus();
return (false);
}
else
{
return (true);
}



Cita:
Iniciado por niconico
Todo me va bien salvo el ultimo alert (observaciones) que no me funciona no tengo ni idea del porque:


<script Language="JavaScript" Type="text/javascript"><!--
function FrontPage_Form1_Validator(theForm)
{

if (theForm.hito.value == "")
{
alert("Escriba un valor para el campo Hito.");
theForm.hito.focus();
return (false);}

if (theForm.tareas.value == "")
{
alert("Escriba un valor para el campo Tareas.");
theForm.tareas.focus();
return (false);}

if (theForm.inicio.value.length < 5)
{
alert("Escriba un valor correcto para el campo Inicio");
theForm.inicio.focus();
return (false);}
if (theForm.observaciones.value == "")
{
alert("Escriba un valor para el campo Observaciones.");
theForm.observaciones.focus();
return (false);}
return (true);
}
</script>

El input de observaciones es :
<INPUT NAME="observaciones" TYPE="TEXT" class="Estilo21">
Estoy cansado de mirarlo y no veo el problema todos me funcionan salvo el campo observaciones, muchas gracias!!
__________________
gerardo
  #3 (permalink)  
Antiguo 02/03/2006, 03:56
Avatar de niconico  
Fecha de Ingreso: enero-2006
Mensajes: 166
Antigüedad: 18 años, 4 meses
Puntos: 0
Muchas gracias por responder pero me sigue dando el problema, es como si no leyera el codigo de javascript de observaciones, no tengo ni idea
  #4 (permalink)  
Antiguo 02/03/2006, 04:09
Avatar de tunait
Moderadora
 
Fecha de Ingreso: agosto-2001
Ubicación: Terok Nor
Mensajes: 16.805
Antigüedad: 22 años, 8 meses
Puntos: 381
Buenas,

asegúrate de que el campo observaciones se encuentra dentro del form, no se haya quedado fuera (detrás del tag de cierre </form>)
  #5 (permalink)  
Antiguo 02/03/2006, 04:55
Avatar de niconico  
Fecha de Ingreso: enero-2006
Mensajes: 166
Antigüedad: 18 años, 4 meses
Puntos: 0
Esta dentro esta todo entre los tags del formulario <form> y </form>, cual puede ser el problema?
  #6 (permalink)  
Antiguo 02/03/2006, 04:58
Avatar de tunait
Moderadora
 
Fecha de Ingreso: agosto-2001
Ubicación: Terok Nor
Mensajes: 16.805
Antigüedad: 22 años, 8 meses
Puntos: 381
Tal vez entonces habría que ver todo el código en conjunto. Déja el código completo de la página (si es muy largo entonces mejor colócalo en un servidor y nos dejas el link)
  #7 (permalink)  
Antiguo 02/03/2006, 05:52
Avatar de niconico  
Fecha de Ingreso: enero-2006
Mensajes: 166
Antigüedad: 18 años, 4 meses
Puntos: 0
Aqui está el código muchas gracias

<HTML>
<HEAD>
<TITLE>Insertar.htm</TITLE>
<link href="css/estilo.css" rel="stylesheet" type="text/css">

<style type="text/css">
<!--
.boton {
width: 90px;
background-color: #CE6131;
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #D6D3CE;
text-align: center;
cursor: hand;
}
.Estilo21 {
font-size: 14;
font-family: Arial, Verdana, Helvetica, sans-serif;
color: #424142;
}
.boton1 { width: 90px;
background-color: #CE6131;
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #D6D3CE;
text-align: center;
cursor: hand;
}
<style type="text/css">
<!--
.Estilo22 {font-size: 24pt}
.Estilo22 {
font-size: 24pt;
font-weight: bold;
font-family: Verdana, Arial, Helvetica, sans-serif;
}


-->
</style>
-->
</style>
</HEAD>
<body background="Marca-de-agua-blanca2.jpg">
<script Language="JavaScript" Type="text/javascript"><!--
function FrontPage_Form1_Validator(theForm)
{

if (theForm.hito.value == "")
{
alert("Escriba un valor para el campo Hito.");
theForm.hito.focus();
return (false);}

if (theForm.tareas.value == "")
{
alert("Escriba un valor para el campo Tareas.");
theForm.tareas.focus();
return (false);}

if (theForm.inicio.value.length < 5)
{
alert("Escriba un valor correcto para el campo Inicio");
theForm.inicio.focus();
return (false);
}
if (theForm.estado.value == "")
{
alert("Escriba un valor para el campo Estado.");
theForm.estado.focus();
return (false);
}
if (theForm.observaciones.value == "")
{
alert("Escriba un valor para el campo Observaciones.");
theForm.observaciones.focus();
return (false);
}
else
{
return (true);
}
}

</script>
<table width="980" height="56" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="980" height="46" colspan="2" align="center" valign="middle" bgcolor="#63CF31"><font color="#424142" class="Estilo12 Estilo34 Estilo22">Nuevo registro</font></td>
</tr>
</table>
<table>
<tr>
<td width="969" align="right">
<img src="logo-pisa-simple2.jpg" width="74" height="29" />
</td>
</tr></table>
<br>
<FORM METHOD="POST" ACTION="insertar.php" onsubmit="return FrontPage_Form1_Validator(this)" language="JavaScript" name="FrontPage_Form1">
<table width="980">
<tr>
<td width="54" align="right" valign="top" class="Estilo21">&nbsp;</td>

<td width="128" align="left" valign="top" class="Estilo21"><strong>Hito:</strong></td>
<td colspan="2" valign="top"><INPUT NAME="hito" TYPE="TEXT" class="Estilo21"><br><br></td>

<tr>
<td align="left" valign="top" class="Estilo21">&nbsp;</td>
<td align="left" valign="top" class="Estilo21"><strong>Tareas:</strong></td>
<td colspan="2" valign="middle"><INPUT NAME="tareas" TYPE="TEXT" class="Estilo21"><br><br></td>
</tr>
<tr>
<td align="right" valign="top" class="Estilo21">&nbsp;</td>
<td align="left" valign="top" class="Estilo21"><strong>Fecha de Inicio:</strong></td>
<td width="156"><INPUT NAME="inicio" TYPE="TEXT" class="Estilo21"><br><br></td>
<td width="622" valign="top">&nbsp;</td>
</tr>
<tr>
<td align="right" valign="top" class="Estilo21">&nbsp;</td>
<td align="left" valign="top" class="Estilo21"><strong>Estado:</strong></td>
<td colspan="2"><select name="eleccion" class="Estilo21">
<option value="No iniciada">No iniciada</option>
<option value="En curso">En curso</option>
<option value="Finalizada">Finalizada</option>
<option value="Descartada">Descartada</option>
<option value="Aplazada">Aplazada</option>
</select><br><br></td>
</tr>
<tr>
<td align="right" valign="top" class="Estilo21">&nbsp;</td>
<td align="left" valign="top" class="Estilo21"><strong>Observaciones:</strong></td>
<td colspan="2"><INPUT NAME="observaciones" TYPE="TEXT" class="Estilo21"><br></td>
</tr>
<tr>
<td height="23">&nbsp;</td>
<td height="23"><input name="SUBMIT" type="SUBMIT" class="boton1" value="Insertar"></td>
<td colspan="2">&nbsp;</td>
</table>
<table width="980">
<tr>
<td height="30"></td>
<td width="116" height="30" align="center"></td>
<td width="93" align="center"><a class="azul" href="contrase.htm"><font size="3" face="Verdana, Arial, Helvetica, sans-serif">Salir</font></a></td>
<td width="121" align="center">&nbsp;</td>
<td height="30">&nbsp;</td>
</tr>
</table>
</FORM>

</BODY>
</HTML>
  #8 (permalink)  
Antiguo 02/03/2006, 08:17
Avatar de tunait
Moderadora
 
Fecha de Ingreso: agosto-2001
Ubicación: Terok Nor
Mensajes: 16.805
Antigüedad: 22 años, 8 meses
Puntos: 381
Ok, el tema es que el script se detiene al dar un error, así que al no retornar un false el form se envía.

El fallo está al validar el campo de tipo select, que lo tienes con un nombre distinto al cual usas para referenciarlo en el script.

lo tienes así
<select name="eleccion" class="Estilo21">

cámbiale el name
<select name="estado" class="Estilo21">

Igual ese campo (estado) te validará siempre tal y como lo tienes (un select no se valida igual que otros campos de tipo text o textarea). Para que te valide también el select deberías hacerlo así

Código:
if (theForm.estado.selectedIndex == 0)
{
alert("Seleccione un valor para el campo Estado.");
theForm.estado.focus();
return (false);
}
Un saludo
  #9 (permalink)  
Antiguo 02/03/2006, 10:06
Avatar de niconico  
Fecha de Ingreso: enero-2006
Mensajes: 166
Antigüedad: 18 años, 4 meses
Puntos: 0
Muchas gracias ahí estaba el problema, 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 01:01.