martes, 21 de marzo de 2017

Formulario que nos pide DNI, nombre, dirección, código postal y lo envía por correo

<!-- 5). Formularios, validación longitud, email, etc. Expresiones regulares. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script>
    function Valida(formulario) {
        /* Validación de campos NO VACÍOS */
        if ((formulario.campo1.value.length == 0) ||
        (formulario.campo2.value.length ==0) ||
        (formulario.cpostal.value.length ==0) ||
        (formulario.dni.value.length ==0) ||
        (formulario.email.value.length ==0)) {
            alert('falta información en los campos');
            return false;
        }  
       
       
        if (isNaN(parseInt(formulario.campo2.value))) {
            alert('el campo2 debe ser un número');
            return false;
        } 
        /* validación del CÓDIGO POSTAL*/
        var ercp=/(^([0-9]{5,5})|^)$/;
        if (!(ercp.test(formulario.cpostal.value))) { 
            alert('El contenido del código postal no es un CÓDIGO POSTAL');
            return false;
        }
        /* validación del DNI */
        var erdni=/(^([0-9]{8,8}\-[A-Z])|^)$/;
        if (!(erdni.test(formulario.dni.value))) { 
            alert('Introduzca un DNI válido con este formato 00000000-Z');
            return false; 
        }
        /* validación del e-mail */
        var ercorreo=/\w+@\w+.\w+/;         
        if (!(ercorreo.test(formulario.email.value))) { 
            alert('Contenido del email no es CORREO ELECTRÓNICO válido.');
            return false;
        }
        /* si no hemos detectado fallo devolvemos TRUE */
        return true;
    }
   
</script>
</head>

<body>
<form name="miFormulario" onsubmit="return Valida(this);" action="mailto:mi@mail.com" >
    Campo1 <input type="text" name="campo1"><br>
    Campo2 <input type="text" name="campo2"><br>
    cpostal <input type="text" name="cpostal"><br>
    dni <input type="text" name="dni"><br>
    email <input type="text" name="email"><br>
    <input type="submit" value="Enviar" name="enviar">
</form>
</body>
</html>

No hay comentarios:

Publicar un comentario