I’m using jqueryvalidation plugin for form validation .On clicking the button i want to make form validation first and ajax request second . how i can achieve this?
Hi @itzbrinth, this simple code might point you in the right direction, hope it helps:
var form = document.getElementById('myForm');
form.addEventListener('submit', function(event) {
// prevent default form submission
event.preventDefault();
// return true or false from a validate function
var validated = validateMyForm();
if (validated) {
// make AJAX request
} else {
alert('please fix the following errors...');
}
});
2 Likes
On clicking submit button first i want the form to validate and then ajax request has to be made using jquery validation plugin .how is it possible?
<div class="form-group">
<label>Name</label>
<input class="form-control" type="text" name="name">
</div>
<div class="form-group">
<label>Email</label>
<input class="form-control" type="email" name="email">
</div>
<div class="form-group">
<label>Phone</label>
<input class="form-control" type="number" name="number">
</div>
<button class="btn btn-success" type="submit" name="sbt">Submit</button>
$("#myform").validate({
rules: {
name: {
required : true
}
},
messages:{
name:"You Must enter your Name"
},
submitHandler: function(form) {
$(form).submit();
}
});
</script>
Topics merged.
The HTML you posted seems incomplete and incorrect. Also the formatting is all wrong. If you need help I would suggest posting all the HTML for the form, format it correctly and then tell us what you tried and what are the specific problems you encountered. Hope that helps,
Andres
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.