I have an ASP.Net web form page on which I've applied JQuery validation. The problem is, when the validation fails, the page still postbacks. I have two textboxes which have required : true
validation.
This is my submit button code :
<button id="btnSubmit" class="btn btn-info" type="button" onserverclick="btnSubmit_Click" onclick="checkValidate(event);" causesvalidation="true"
runat="server">
<i class="icon-ok bigger-110"></i>Submit
</button>
This is JQuery's validate method :
$(function() {
$('#validationForm').validate({
errorElement:'span',
errorClass: 'help-inline',
focusInvalid: false,
//debugger;
rules: {
<%=txtName.UniqueID %>: {
required: true
},
<%=txtDescription.UniqueID %>: {
required: true
}
},
messages: {
<%=txtName.UniqueID %>: {
required: "Please provide a valid name."
},
<%=txtDescription.UniqueID %>: {
required: "Please specify a description."
},
},
invalidHandler: function (event, validator) { //display error alert on form submit
$('.alert-error', $('.login-form')).show();
},
highlight: function (e) {
$(e).closest('.control-group').removeClass('info').addClass('error');
},
success: function (e) {
$(e).closest('.control-group').removeClass('error').addClass('info');
$(e).remove();
},
submitHandler: function (form) {
},
invalidHandler: function (form) {
}
});
})
And this is the method which checks for the validation on button click:
function checkValidate(evt) {
// Validate the form and retain the result. //
var isValid = $('#validationForm').valid();
// // If the form didn't validate, prevent the form submission. //
//debugger;
if (!isValid){
evt.preventDefault();
evt.stopPropagation();
return false; }
else
return true;
}
This is the rendered HTML of my button :
<button onclick="checkValidate(event); __doPostBack('ctl00$ContentPlaceHolder1$btnSubmit','')" id="ContentPlaceHolder1_btnSubmit" class="btn btn-info" type="button">
<i class="icon-ok bigger-110"></i>Submit
</button>
Also, I've put the debugger in the checkValidate() method, & found that everything is working fine there, but after the end of method, the page postbacks. I ran the JQuery valid() method on browser's console, and it's also running fine, returning the correct values.
When Textbox(es) are empty :
When Textboxes are filled :
Where exactly is the problem? How can I stop page from postback when validation fails?