Hola a todos. Estoy haciendo un formulario simple con validación de campos (soy estudiante, recién empiezo a aprender) y se me complicó con el tema de validar el e-mail (que tenga arroba y .com) y hacer que el nombre contenga sólo letras.
Di vueltas por muchos foros pero no entiendo las explicaciones, y cuando pruebo de copiar los códigos de ejemplo, se me rompe todo.
Acá adjunto el código entero a ver si alguien me puede dar una mano por favor. Gracias!
<!DOCTYPE html>
<html>
<head>
<title>Formulario de consulta</title>
<style type="text/css">
body {
background-color: #bebcbe;
}
h1 {
text-align: center;
}
#principal {
border: groove 8px;
padding:20px;
background-color: #b8dfdc;
position:absolute;
margin-left:430px;
}
</style>
<script type="text/javascript">
function validarFormulario()
{
var a=document.forms["miFormulario"]["nombre"].value;
if (a==null || a=="")
{
alert("Debe ingresar su nombre");
return false;
}
if (a!=document.forms["miFormulario"]["nombre"].value.match(/[a-z]/))
{alert('Solo letras por favor');
return false;}
var b=document.forms["miFormulario"]["apellido"].value;
if (b==null || b=="")
{
alert("Debe ingresar su apellido");
return false;
}
var c=document.forms["miFormulario"]["edad"].value;
if (c==null || c=="")
{
alert("Debe ingresar su edad");
return false;
}
if (isNaN(c))
{
alert("Debe ingresar un número válido");
return false;
}
else if (c==0)
{alert("Sabemos que no tiene 0 años")
return false;
}
else if(c<18)
{alert("Debe ser mayor de 18 años")
return false;
}
var e=document.forms["miFormulario"]["correo"].value;
if (e==null || e=="")
{
alert("Debe ingresar su correo");
return false;
}
var f=document.forms["miFormulario"]["Consulta"].value;
if (f==null || f=="")
{
alert("Debe ingresar una consulta");
return false;
}
else {
alert ("GRACIAS!! CONSULTA RECIBIDA.");
return true;
}
var e=document.forms["miFormulario"]["Correo"].value;
if( !(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)/.test()) ) {
return false;
}
}
</script>
</head>
<body>
<div>
<h1>Formulario de Consulta</h1>
<div id="principal">
<form name="miFormulario" action="formulario.asp" method="post" onsubmit="return validarFormulario()">
Nombre: <br />
<input type="text" name="nombre" class="formucolor" maxlength="15" /> <br />
Apellido: <br />
<input type="text" name="apellido" class="formucolor" maxlength="10" /> <br />
Edad: <br />
<input type="text" name="edad" maxlength="2" size="2"/> <br />
Correo: <br />
<input type="text" name="correo" maxlength="20" /> <br /><br />
Consulta: <br />
<textarea name="Consulta" rows="6" cols="50"></textarea> <br />
<br />
<input type="submit" name="enviar" value="Enviar" />
<input type="reset" name="limpiar" value="Borrar sin enviar" />
</form>
</div>
</div>
</body>
</html>