当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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>

网友答案:

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

<form id="whatever" ... onsubmit="return doValidation();">
分享给朋友:
您可能感兴趣的文章:
随机阅读: