Reputation: 4816
I have a for that I am submitting with this button:
<button id="registerButton" type="submit" name="submit" style="margin-left: 300px;">Click to Complete</button>
I am doing error checking with this jQuery:
$('#registerButton').bind('click',function(){
if($('#formElem').data('errors')){
alert('Please correct the errors in the Form');
return false;
}
});
When I go to submit the form it will pop up with the alert if there are errors detected on the form. However, if there are no errors the button doesn't do anything. What am I doing wrong?
Upvotes: 1
Views: 1891
Reputation: 814
Just to provide you with a complete minimal example that works for me, in firefox, internet explorer, chrome and opera. Hope that helps.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
$("#frm").submit(function(e){
if($(this).data("errors")){
alert("errors");
return false;
}
return true;
});
});
</script>
</head>
<body>
<form id="frm" action="#sent" data-errors="have errors">
<input type="submit" />
</form>
</body>
</html>
Upvotes: 0
Reputation: 79021
Bind the validation on the submit event of the form.
$("#formid").submit(function(e) {
if($(this).data('errors')){
//display error
e.preventDefault(); //stop the submit
} else {
//do something or nothing
//but it will submit the form
}
});
Upvotes: 2
Reputation: 1402
Looks like you need to add an else {return true;}
to your function.
Upvotes: 0