Skip to content

Instantly share code, notes, and snippets.

@biohzrdmx
Created July 21, 2014 14:39
Show Gist options
  • Select an option

  • Save biohzrdmx/7680abeb221dc9adb1ee to your computer and use it in GitHub Desktop.

Select an option

Save biohzrdmx/7680abeb221dc9adb1ee to your computer and use it in GitHub Desktop.
Form handlers (validator + jquery-form)
$('form[data-submit=validate]').on('submit', function() {
var form = $(this);
return form.validate({
error: function(fields) {
fields.each(function() {
var field = $(this);
field.closest('.form-group').addClass('has_error');
field.on('focus', function() {
field.closest('.form-group').removeClass('has_error');
field.off('focus');
});
});
}
});
});
$('form[data-submit=ajax]').each(function() {
var form = $(this);
form.ajaxForm({
dataType: 'json',
beforeSubmit: function() {
return form.validate({
success: function() {
form.find('input, select').prop({ disabled: true });
form.find('button[type=submit]').prop({ disabled: true }).loading({ text: 'Enviando...' });
},
error: function(fields) {
fields.each(function() {
var field = $(this);
field.closest('.form-group').addClass('has_error');
field.on('focus', function() {
field.closest('.form-group').removeClass('has_error');
field.off('focus');
});
});
}
});
},
success: function(response) {
form.clearForm();
form.find('input, select').prop({ disabled: false });
form.find('button[type=submit]').prop({ disabled: false }).loading('done');
$.alert('Gracias por tus comentarios');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment