martes, 21 de marzo de 2017

Formulario nos pide datos, mail, dni, correo y nos lo envía al correo electrónico

<!-- 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 el campo1');
            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('Contenido del dni no es un DNI válido.');
            return false; 
        }
        /* validación del e-mail */
        var ercorreo=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;         
        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