我试图验证四个不同的字段。最后两个验证行为正确,但前两个不起作用(它们应该只是必需的)。如果我按下提交而不输入任何内容,则只有最后两个字段显示错误。
这里是有问题的jQuery:
$("#advertisePost").validate({
rules: {
advRetailerName: {
required: true
},
advName: {
required: true
},
advPhone: {
required: true,
phoneUS: true
},
advEmail: {
required: true,
email: true
}
},
messages:{
advRetailerName: {
required: "This is required."
},
advName: {
required: "This is required."
},
advPhone: {
required: "This is required.",
phoneUS: "This is an invalid phone number."
},
advEmail: {
required: "This is required",
email: "This is an invalid email"
}
}
});
这是实际的表格本身:
<form action="php/advertisePost.php" method="POST" id="advertisePost">
<table>
<tr>
<td>Retailer Name</td><td><input type="text" id="advRetailerName" name="advRetailerName" class="required"/><br/><label for="advRetailerName" class="error" generated="true"></label></td>
</tr>
<tr>
<td>Your Name</td><td><input type="text" id="advName" name="advName" class="required"/><br/><label for="advName" class="error" generated="true"></label></td>
</tr>
<tr>
<td>Phone Number</td><td><input type="text" id="advPhone" name="advPhone" class="required"/><br/><label for="advPhone" class="error" generated="true"></label></td>
</tr>
<tr>
<td>Email Address</td><td><input type="text" id="advEmail" name="advEmail" class="required"/><br/><label for="advEmail" class="error" generated="true"></label></td>
</tr>
</table>
<input type="button" value="Submit" id="advSubmit"/>
</form>
我甚至尝试将所需的类添加到每个字段。我知道它也不是消息处理问题。前两个字段根本不遵循验证规则。有什么帮助吗?
答案 0 :(得分:1)
尝试这样
$.validator.addClassRules({
advPhone: {
required: true,
phoneUS: true
},
advEmail: {
required: true,
email: true
}
});
$("#advertisePost").validate();
然后添加前两个所需的类
或者,您可以只执行验证行并添加类电子邮件,并且需要最后两个
答案 1 :(得分:0)
如果您只想验证前两个字段是否必需,则可以在各自的输入字段中设置class =“required”,并在jquery中设置advRetailerName:“required”。以下是验证文档:http://docs.jquery.com/Plugins/Validation
答案 2 :(得分:0)
似乎是验证插件中的错误。当你只是要求“必需”参数时,它可能不理解复杂风格的规则分配?你尝试过这种简单的方法:
rules: {
advRetailerName: "required",
(...)
}