我有一些javascript检查表单用户输入。我在检查返回false后点击提交,但仍然提交。我想停止表单提交。
javascript代码
<script>
function validation(){
var emailFilter=/^.+@.+\..{2,3}$/;
var str=document.form.email.value;
if (!(emailFilter.test(str))) {
document.getElementById("emailStatus").innerHTML = '<p class="notify-valid"> <p class="notify-valid" style="font-family: Kanit;"><i class="icon ion-close-round" style="color:#FF0004;!important"></i> Wrong Email</p></p>';
document.getElementById('hideMessage').style.display = 'block';
return false;
}
}
</script>
html代码
<form name="form" action="action.php" method="post" onsubmit="return validation()">
<div class="form-group">
<label class="sr-only" for="contact-email">Your Email : </label>
<input type="text" name="email" placeholder="Enter Email..." class="contact-email form-control" id="contact-email">
<div class="message" id="emailStatus">
<div id="hideMessage" style="display:none"></div>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="contact-message">Your message ซ</label>
<textarea name="message" placeholder="message" class="contact-message form-control" id="contact-message"></textarea>
</div>
<button type="submit" class="btn">Submit</button>
</form>