I am trying to add validation to my form with jQuery Validation Plugin, but I'm having a problem where the plugin puts the error messages when I'm using input groups.
$('form').validate({
rules: {
firstname: {
minlength: 3,
maxlength: 15,
required: true
},
lastname: {
minlength: 3,
maxlength: 15,
required: true
}
},
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
}
});
My code: http://jsfiddle.net/hTPY7/4/
add radio-inline fix to this https://stackoverflow.com/a/18754780/966181
DARK_DIESEL's answer worked great for me; here's the code for anyone who wants the equivalent using glyphicons:
This makes up the fields
For full compatibility with Bootstrap 3 I added support for input-group, radio and checkbox, that was missing in the other solutions.
Update 10/20/2017: Inspected suggestions of the other answers and added additional support for special markup of radio-inline, better error placement for a group of radios or checkboxes and added support for a custom .novalidation class to prevent validation of controls. Hope this helps and thanks for the suggestions.
After including the validation plugin add the following call:
This works for all Bootstrap 3 form classes. If you use a horizontal form you have to use the following markup. This ensures that the help-block text respects the validation states ("has-error", ...) of the form-group.
I use forms designed only with Twitter Bootstrap 3. I set defaults functions for validor and run only validate method with rules. I use Icons from FontAweSome, but you can use Glyphicons as in doc example.
Done. After run validate function:
JSFiddle example
Another option is placing an error container outside of your form group ahead of time. The validator will then use it if necessary.