Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: <Script> function to validate <form> input?  (Read 4810 times)

0 Members and 1 Guest are viewing this topic.

Tomwatts

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows XP
    <Script> function to validate <form> input?
    « on: November 24, 2014, 09:01:12 AM »
    I have the below code:

    <form name="myForm" action="C:\dev\page2.html" onsubmit="return validateForm()" method="post">
    ABCD 1234: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>

    <script>
    function validateForm() {
        var x = document.forms["myForm"]["fname"].value;
        if (x == null || x == "") {
            alert("You haven't completed this task yet!");
       return false;
        }
    }
    </script>

    In the input, the user needs to type ABCD 1234 as per the name of the form field, If the user inputs no information they get a pop up saying they haven't completed the task yet, however I need it so if the user inputs the ABCD 1234 they'll be granted access to the next page, and if they put in anything different it will return that pop up.

    Can anyone help me please?