如果只是bootstrap3和jquery.validation的话,参考Bootstrap 3 with jQuery Validation Plugin只需很简单的:
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
// override jquery validate plugin defaults | |
$.validator.setDefaults({ | |
highlight: function(element) { | |
$(element).closest('.form-group').addClass('has-error'); | |
}, | |
unhighlight: function(element) { | |
$(element).closest('.form-group').removeClass('has-error'); | |
}, | |
errorElement: 'span', | |
errorClass: 'help-block', | |
errorPlacement: function(error, element) { | |
if(element.parent('.input-group').length) { | |
error.insertAfter(element.parent()); | |
} else { | |
error.insertAfter(element); | |
} | |
} | |
}); |
再加上asp.net mvc4的话,情况就有点不同了,mvc4会在errorElement
外面在加一层,并移除内层元素的class
的内容:
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
<span class="field-validation-error" data-valmsg-for="UserName" data-valmsg-replace="true">
<span for="UserName" class="">The User name field is required.</span>
</span>
我的解决办法是将.field-validation-error
赋予和.'help-block
相同的css样式
Comments