Skip to content

Instantly share code, notes, and snippets.

@agustinprosperi
Last active November 1, 2017 11:57
Show Gist options
  • Save agustinprosperi/60944fa4ca8a76bba01c to your computer and use it in GitHub Desktop.
Save agustinprosperi/60944fa4ca8a76bba01c to your computer and use it in GitHub Desktop.
jquery short examples
// wait for images to load
$(window).bind("load", function() {
// code here, masonry for example
});
// validateEmail
function validateEmail(email) {
var re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return re.test(email);
}
// Ajax call
$(".register-form").submit(function(e){
if( $(currentFormId).hasClass("sending") ) return false;
//if no sending yet, send
e.preventDefault();
currentFormId = '#'+$(this).attr("id");
var formData = $(this).serialize();
$(currentFormId).addClass("sending");
$.ajax({
type: 'POST',
url: ajax_object.ajax_url,
data: formData,
success: function(data, textStatus, jqXHR){
var $messagebox = $(currentFormId).find(".form_messages");
$messagebox.hide();
if(data.status == 'success'){
$messagebox.html( data.message );
$(currentFormId)[0].reset();
}else if(data.status == 'error'){
$messagebox.html( data.message );
}else{
$messagebox.html( $(".tecnical_error").first().html() );
}
$messagebox.slideDown(300);
$(currentFormId).removeClass("sending");
},
error: function(jqXHR, textStatus, errorThrown){
var $messagebox = $(currentFormId).find(".form_messages");
$messagebox.hide();
$messagebox.html( $(".tecnical_error").first().html() );
$messagebox.slideDown(300);
$(currentFormId).removeClass("sending");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment