jquery form submit validation with ajax code example
Example 1: jQuery AJAX form submit
$("#myFormID").submit(function(e) {
e.preventDefault();
var form = $(this);
var url = form.attr('action');
$.ajax({
type: "POST",
url: url,
data: form.serialize(),
success: function(data){
console.log(data);
}
});
});
Example 2: jquey validate and submit form using jquery ajax
$("#form_id").validate({
rules:{
name:"required",
email:{email: true,required: true},
mobile:{required : true,minlength:10},
},
messages: {
name:"Please fill name",
email:{email:"Enter Valid Email!",
required:"Enter Email!"
},
mobile:{minlength:"Please enter Valid Mobile No.",
required:"Please enter Mobile No."
},
},
submitHandler: function(form){
$.ajax({
url: form.action,
type: form.method,
data: $(form).serialize(),
success: function(response) {
$('#answers').html(response);
}
});
}
});