mercredi 22 avril 2015

JavaScript - Outputting a line with Document Write

I am trying to output a line with the javascript Document.Write function, however, on Submitting the form, the line just flashes and disappears

any thoughts?

<!DOCTYPE html>

<html>
    <head>
        <title>Form Validation</title>
    </head>
    <body>

        <script type="text/javascript">
            function checkAge() {

                var x = document.forms['myForm']['age'].value;

                if(x < 18) {
                    document.write("<p>You are too Young. Form NOT submitted</p>");
                } else {
                    document.write("Form sent successfully");
                }
            }

            function tips() {
                alert("Please enter your Name in the name Box, \nand Your age in the Age box.\nYou have to be Over 18 to submit the form");
            }
        </script>

        <form id="myForm" action="formvalidation.html" onsubmit="checkAge();">    
            Name: <input type="text" name="text" id="name">
            Age: <input type="text" name="age" id="age">
            <input type="submit" value="Submit">
            <input type="button" value="Help" id="hBtn" onclick="tips();">
        </form>
    </body>

</html>

Aucun commentaire:

Enregistrer un commentaire