jquery form submit validation with ajax code example

Example 1: jQuery AJAX form submit

// jQuery ajax form submit example, runs when form is submitted
$("#myFormID").submit(function(e) {
    e.preventDefault(); // prevent actual form submit
    var form = $(this);
    var url = form.attr('action'); //get submit url [replace url here if desired]
    $.ajax({
         type: "POST",
         url: url,
         data: form.serialize(), // serializes form input
         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);
	}            
      });		
}
});