domejercicio3.html 1.97 KB
Newer Older
1 2 3 4 5
<!DOCTYPE html>

<html lang="en">
<head>

6 7 8 9 10 11
    <style>
        #error{
            color: red;
        }
    </style>

12 13 14
</head>
<body>
    <form action="mailto:me@fakeemail.com" id="send" onsubmit="return checkForm();">
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
            <fieldset>
                <legend>Personal details</legend>
                <p>
                    <label>
                        Full name:
                        <input type="text" name="fullname" id="fullname">
                    </label>
                </p>
                <p class="errormsg" id="nameerrormsg">Please enter your name above</p>
                <p>
                    <label>
                        Street Address:
                        <input type="text" name="streetaddr" id="streetaddr">
                    </label>
                </p>
                <p class="errormsg" id="addrerrormsg">Please enter your street address</p>
            </fieldset>
            <input type="submit" value="Submit it!">
33
        </form>     
34
        <div id="error"> </div>
35 36 37
</body>
<script>

38 39 40 41 42 43 44 45 46
    function checkForm(){

        var valido = true;

        var campo1 = document.getElementById('fullname');

        var campo2 = document.getElementById('streetaddr');

        var error = document.getElementById('error');
47

48
        var mensajeError= [];   
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            if((campo2.value=='' || campo2.value== null) && (campo1.value == '' || campo1.value == null)){
                mensajeError.push('nameerrormsg');
                valido = false;
            }else{
                if(campo1.value == '' || campo1.value == null){
                    mensajeError.push('nameerrormsg');
                    valido = false;
                }else{
                    if(campo2.value=='' || campo2.value== null){
                        mensajeError.push('StreetAddresserrormsg');
                        valido = false;
                    }
                }

            }

            error.innerHTML= mensajeError;

            return valido;
69
   
70 71
    

72 73 74 75 76
}


</script>
</html>