webshan
webshan

Reputation: 1

form getting submitted even if validation fails

I have a page where i am disabling the button after submit for 10 seconds and if validation succeeds form is submitted. However, the form is getting submitted even though validation is returning false.

I tried using

<input type="submit">

instead of image still same issue.

Below is the code:

<html>
    <head>
        <script>
            function enableMe(myBtn) {
                myBtn.disabled = false;
            }
            function doValidation() {
                document.getElementById('hidden1').value = 'true';
                return false;
            }
        </script>

    </head>
    <body>
        <form id="loginForm" method="post" action="https://www.mySite.com/authService">
            <label for="userid">Username</label><br/>
            <input type="text" value="" id="userid" name="userid"/>
            <br/>

            <label for="password">Password</label>
            <br/>
            <input type="password" value="" id="password" name="password"/>
            <br/>
            <br/>

            <input type="image" id="submitBtn" src="btn_login.gif"
                   onsubmit="this.disabled=true; setTimeout(enableMe,10000,this); return doValidation();">
            <input type="hidden" id="hidden1" name="hidden1" value="false"/>
        </form>
    </body>
</html>

Upvotes: 0

Views: 378

Answers (1)

Billy Moon
Billy Moon

Reputation: 58531

Validation should be attached to form, not input element...

<form id="whatever" ... onsubmit="return doValidation();">

Upvotes: 2

Related Questions