Foros del Web » Programando para Internet » Javascript »

No me valida el form!

Estas en el tema de No me valida el form! en el foro de Javascript en Foros del Web. Buenas Tengo este formulario, el cual intento validar cada campo con javascript, pero no me da ni la hora jajaja, pasa directo de una aunque ...
  #1 (permalink)  
Antiguo 03/11/2009, 16:42
 
Fecha de Ingreso: agosto-2003
Mensajes: 906
Antigüedad: 20 años, 8 meses
Puntos: 3
No me valida el form!

Buenas

Tengo este formulario, el cual intento validar cada campo con javascript, pero no me da ni la hora jajaja, pasa directo de una aunque no ponga nada, xq?????? que puedo hacer o que esta mal? (NO HAGAN CASO A LOS HIDDEN Y ESOS CAMPOS, TIENEN QUE VER CON SALESFORCE)

El JS

Código PHP:
<script  type="text/javascript">
function 
Valida(form) {
if(
form.state.value == '') {
alert('Please enter your state');
form.state.focus();
return 
false;
}
 
if(
form.first_name.value == '') {
alert('Please enter your first name');
form.first_name.focus();
return 
false;
}
if(
form.email.value == '') {
alert('Please enter your email');
form.email.focus();
return 
false;
}
return 
true;

</script> 
el form

Código PHP:
<form action="paginaadondemanda.php" method="POST" class="formheader" name="form" onSubmit="return Valida(this);">
    <
input type=hidden name="oid" value="00D400000009u8W">
    <
input  type=hidden id="00N40000001IMeh" maxlength="255" name="00N40000001IMeh" size="20"   value="www.satbed.com-sign_up_form"/>
    <
input type=hidden name="retURL" value="http://www.satbed.com/">
    <
table width="200" border="0" align="center" cellpadding="0" cellspacing="2">
      <
tr>
        <
td valign="baseline"><input name="first_name" type="text" class="form1"  id="first_name" size="15" maxlength="40" value="Name:"  /></td>
        <
td valign="baseline"><input name="email" type="text" class="form1"  id="email" size="20" maxlength="80" value="Email:" /></td>
        <
td valign="baseline"><input name="state" type="text" class="form1"  id="state"  style="width:35px"  value="State:" size="2" maxlength="2" title="Please insert state here" ></td>
        <
td valign="baseline" ><input type="image" src="img/boton-form.gif" name="boton_submit" border="0" vspace="0" hspace="5" alt="Enviar" style=" width:19px; position:relative; top:3px; left:2px; display:inline; padding:0; margin:0" /></td>
      </
tr>
    </
table>
  </
form
  #2 (permalink)  
Antiguo 03/11/2009, 17:09
Avatar de deirdre  
Fecha de Ingreso: mayo-2009
Mensajes: 690
Antigüedad: 15 años
Puntos: 45
Respuesta: No me valida el form!

Hola SeNdEr2003

Quizás este validador de campos te venga bien (haz un copiar y pegar en un html limpio y lo pruebas):

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validacion sencilla de campos de formulario</title>

<script type="text/javascript">
function validate_email(field,alerttxt)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {alert(alerttxt);return false;}
  else {return true;}
  }
}

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(nombre,"Por favor, escriba su nombre")==false)
  {nombre.focus();return false;}

  if (validate_email(email,"Por favor, escriba su email")==false)
    {email.focus();return false;}

  if (validate_required(profesion,"Por favor, escriba su profesion")==false)
  {profesion.focus();return false;}
  
  if (validate_required(domicilio,"Por favor, escriba su domicilio")==false)
  {domicilio.focus();return false;}

  if (validate_required(mensaje,"Por favor, escriba su mensaje")==false)
  {mensaje.focus();return false;}
  
  }
}
</script>
<style type="text/css">
#formulario {
	width: 340px;
	height: 320px;
	text-align: left;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	background-color:#e1e1e1;
	padding: 8px;
	border: 1px solid #484848;
}
.caja {
	width: 310px;
	text-align: left;
}
input {
	width: 300px;
}
textarea {
	width: 300px;
}
.boton {
	width: 80px;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	*margin-left: -20px; /* necesario para ie */
}
li {
	width: 300px;
	list-style-type: none;
	margin-left: -20px;
	margin-top: 8px;
}
label {
	float: left;
	clear: both
}
</style>
</head>

<body>
<div id="formulario">
	<form action="enviar.php" onsubmit="return validate_form(this)" method="post" class="caja">
	<ul>
		<li><label>Nombre:</label> <input type="text" name="nombre" size="30" /></li>
		<li><label>Email:</label> <input type="text" name="email" size="30" /></li>
		<li><label>Profesion:</label> <input type="text" name="profesion" size="30" /></li>
		<li><label>Domicilio:</label> <input type="text" name="domicilio" size="30" /></li>
		<li><label>Mensaje:</label> <textarea name="mensaje" cols="20" rows="4"></textarea></li>
		<li><input type="submit" value="Enviar" class="boton" /></li>
	</ul>
	</form>
</div>
</body>

</html> 
Bye
  #3 (permalink)  
Antiguo 03/11/2009, 17:28
 
Fecha de Ingreso: agosto-2003
Mensajes: 906
Antigüedad: 20 años, 8 meses
Puntos: 3
Respuesta: No me valida el form!

Hola mira, pude hacerlo con mi code, el tema es que necesito dejar mandar el form solo sino esta en blanco y sino tiene la palabra Name:, por ej trate hacer:

Código PHP:
if(form.first_name.value == '' && == 'Name:') {
alert('Please enter your first name');
form.first_name.focus();
return 
false;

Como podria hacerlo andar?....porque el name le indica al user que poner...
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 14:18.