Skip to content

Instantly share code, notes, and snippets.

@leowebguy
Last active February 18, 2020 15:26
Show Gist options
  • Save leowebguy/f8f252fce0327d899bc2be04dc82ead1 to your computer and use it in GitHub Desktop.
Save leowebguy/f8f252fce0327d899bc2be04dc82ead1 to your computer and use it in GitHub Desktop.
jquery validate + ajax on submitHandler
function submitShare() {
$('form[name=share]').submit();
};
$("form[name=share]").validate({
submitHandler: function (form) {
var frm = $('[name=share]').serializeArray();
var data = {};
$.each(frm, function(i,v) {
data[v.name] = v.value
});
data.text = $('#emailText').html();
$.ajax({
type: "POST",
url: "../process.php",
data: data,
success: function(msg){
$('.toggleHide').each(function() {
$(this).hide();
});
$('.toggleShow').each(function() {
$(this).show();
});
},
dataType: "html"
});
},
rules: {
yourname: {
required: true
},
youremail: {
required: true,
email: true
},
friendname: {
required: true
},
friendemail: {
required: true,
email: true
}
},
messages: {
yourname: {
required: "Name is required."
},
youremail: {
required: "Email is required.",
email: "Enter a valid email."
},
friendname: {
required: "Friend’s name is required."
},
friendemail: {
required: "Friend’s email is required.",
email: "Enter a valid email."
}
},
errorPlacement: function(error, element) {
error.insertAfter(element);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment