【发布时间】:2010-08-04 15:55:54
【问题描述】:
我很难让 jQuery Validation 插件按我的意愿工作。
我拥有的是一个包含几个必需输入的表单,规则如下所示:
$("#makeEvent").validate({
onfocusout: false,
rules: {
name: {
required: true,
minLength: 2,
maxLength: 100
},
host: {
required: true,
minLength: 2,
maxLength: 100
},
startDate: {
required: true,
date: true
},
endDate: {
required: true,
date: true
},
desc: {
required: true,
minLength: 10,
maxLength: 255
},
webpage: {
required: false,
url: true
},
email: {
required: true,
email: true
},
}
});
现在我有一个自定义的 .click() 调用,我想在其中验证表单,然后在允许用户发送表单之前为用户显示预览。请看下面的函数:
$('#submitPreview').click(function() {
if($("#makeEvent").valid() === false) {
//What to do if validation fails
} else if($("#makeEvent").valid() === true) {
//Show the preview and let the user either make changes or submit the final result
}
});
但是发生的情况是该函数仅验证第一个输入(名称),甚至验证不正确(规则要求至少 2 个字符,但仍然只输入 1 个字符进行验证。如果没有字符,则仅返回 false完全添加)。
也许我的方法不是最好的,所以我愿意接受任何建议。如果你想看看杂乱无章的源代码,你可以在这里看到这个函数:http://event.gusthlm.se/make.php
【问题讨论】: