JQuery 验证插件:确保表单字段不是特定值
我正在使用 Jquery 验证插件,源自此处。
它对于必填字段、电子邮件等功能良好,但是我想为某些表单字段设置一些特定的验证规则。例如,我有一个选择表单字段,其中包含以下选项...
<select name="salutation" id="salutation" class="conditional">
<option value="0">--- Please Select ---</option>
<option value="1">--- MR ---</option>
<option value="2">--- MRS ---</option>
</select>
然后,如果尚未选择称呼,我想使用验证插件显示一条消息..IE
$("#userForm").validate({
// Validate Fields On Blur
onfocusout: function(element){
this.element(element);
},
// Set Specific Rules for Validation
rules:{
salutation: {
NEQ:0
}
},
messages:{
salutation: {
NEQ: "please select a salutation"
}
}
});
想知道是否有人对如何自定义有任何想法插件允许这样的事情。非常感谢
i am using the Jquery validation plugin, sourced from here.
It is function well for required fields, email etc, however I would like to set up some specific validation rules for certain form fields. For example, I have a select form field which contains the following options...
<select name="salutation" id="salutation" class="conditional">
<option value="0">--- Please Select ---</option>
<option value="1">--- MR ---</option>
<option value="2">--- MRS ---</option>
</select>
I would then like to use the validation plugin to display a message if a salutation has not been selected..I.E
$("#userForm").validate({
// Validate Fields On Blur
onfocusout: function(element){
this.element(element);
},
// Set Specific Rules for Validation
rules:{
salutation: {
NEQ:0
}
},
messages:{
salutation: {
NEQ: "please select a salutation"
}
}
});
Wondering if anyone had any ideas on how to customize the plugin to allow such things. Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,您可以使用 range
you could use range in this case
将“请选择”选项的值保留为空,并通过为选择字段分配
required
类来使其成为必填字段。这应该足以确保选择其他选项之一。请注意,它将显示默认消息:“此字段为必填字段。”而不是您的自定义消息,但您也可以对其进行调整。Leave the value empty for the "Please select" option and make the select field required by assigning it the class
required
. That should be enough to ensure that one of the other options is chosen. Note that it will have the default message: "This field is required." instead of your custom message, but you could adjust that as well.