我有一些<select>使用所选插件的输入,我想在客户端验证为"必需".由于"selected"隐藏了实际的select元素并创建了一个包含div和spans的小部件,因此本机HTML5验证似乎无法正常工作.表单将不会提交(这是好的),但错误消息未显示,因此用户不知道什么是错的(这不好).
我已经转向jQuery验证插件(我计划最终使用它)但到目前为止还没有任何运气.这是我的测试用例:
<form>
<label>Name: <input name="test1" required></label>
<label>Favorite Color:
<select name="test2" required>
<option value=""></option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
</label>
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function(){
$('select').chosen();
$('form').validate();
});
Run Code Online (Sandbox Code Playgroud)
这样可以select使用空值,而无需验证或显示错误消息.当我注释掉这chosen()条线时,它运行正常.
如何chosen()使用jQuery验证插件验证输入,并显示无效的错误消息?
试图弄清楚为什么我的收件人multiselect没有在表单提交上验证.应至少选择1人.我已将它设置为必需true但仍未显示错误.
JS:
var validateform = $("#pmForm").validate({
rules: {
recipient: {
required: true
},
bcc: {
required: true
},
subject: {
required: true
},
message: {
required: true
}
},
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'You missed 1 field. It has been highlighted.'
: 'You missed ' + errors + ' fields. They have been highlighted.';
$('.box .content-form').removeAlertBoxes();
$('.box .content-form').alertBox(message, {type: 'warning', icon: true, noMargin: false});
$('.box …Run Code Online (Sandbox Code Playgroud)