Last active
December 22, 2015 13:28
-
-
Save andheiberg/6479039 to your computer and use it in GitHub Desktop.
Form validation with jQuery (and twitter bootstrap)
http://testandheiberg.tumblr.com/post/60573208864/form-validation-with-jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.validator.setDefaults({ | |
errorPlacement: function(error, element) { | |
// hide any previous help... this could be "not visible to others" | |
$(element).closest('.controls').find('.help-inline').addClass('hidden'); | |
// if the input has a prepend or append element, put the validation msg after the parent div | |
if ($(element).parent().hasClass('input-prepend') || $(element).parent().hasClass('input-append')) { | |
error.insertAfter($(element).parent()); | |
// else just place the validation message immediatly after the input | |
} | |
else if ($(element).attr('type') === 'checkbox') { | |
$(element).closest('.controls').append(error); | |
} else { | |
error.insertAfter($(element)); | |
} | |
}, | |
errorElement: "span", // contain the error msg in a small tag | |
errorClass: "help-inline", | |
highlight: function(element) { | |
$(element).closest('.control-group').addClass('error'); // add the Bootstrap error class to the control group | |
}, | |
success: function(element) { | |
$(element).closest('.control-group').removeClass('error'); // remove the Boostrap error class from the control group | |
$(element).closest('.controls').find('.help-inline').removeClass('hidden'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment