如果值为0,则Jquery formvalidation是必填字段
Jquery formvalidation required field if value is 0
我目前在页面上插入了jquery表单验证,我目前在验证状态下拉列表时遇到问题,视图是如何构建的。用户选择国家/地区,然后返回控制器,返回链接到该国家/地区的状态,并构建状态下拉列表。在选择国家/地区之前,state下拉列表的选定值为0,根据我的理解,文本为"Please choose a state"。Jquery表单验证将看到0并假设它是一个值,而在我的情况下,这不是一个值。我需要用户选择一个更大的值0。所以当用户按下提交时,如果状态选择值为0,我需要显示错误消息,否则继续。这就是我目前拥有的
$('#myForm').validate({ // initialize the plugin
rules: {
"UserDetails.SelectedCountry": "required",
"UserDetails.SelectedState": {
required: function (element) {
var value = $("#UserDetails_SelectedState").val() == 0;
if (value) { // If the value is true I need to return false to make it required
alert("here");
return false;
} else { // Otherwise value is greater then 0 ok to carry on,
return true;
}
}
}
"UserDetails.Postcode": "required"
},
messages: {
"UserDetails.SelectedCountry": "Please choose your country",
"UserDetails.SelectedState": "Please choose your state",
"UserDetails.Postcode": "Please enter your postcode",
}
});
是否有人能够告诉/展示如何针对0进行验证。
**更新**
我今天一直在断断续续地与之斗争,正如你从我的jquery标记中看到的那样,如果它是真的,我会返回false。我看到警报消息"here",但字段没有用红色突出显示,也没有显示错误消息"Please choose your state",有人能想到它可能是什么吗?
您可以将"Please choose a state"值设置为="并禁用它。
像这样的
<select>
<option value="" disabled>Please Choose a State</option>
<option value="1">1</option>
</select>
将您的验证更改为:
var value = $("#UserDetails_SelectedState").val()
if (value ==0 || value=="0") { // If the value is true I need to return false to make it required
alert("here");
return false;
} else { // Otherwise value is greater then 0 ok to carry on,
return true;
}
相关文章:
- 如果值为0,则Jquery formvalidation是必填字段
- 为什么在使用 formValidation.js 时需要指定字段的行属性
- formvalidation.io-如何跳过验证空字段
- formvalidation.io外部提交按钮
- Fuelux wizard and formvalidation.io - an ajax
- formValidation(旧的BootstrapValidator)破坏了我通过Ajax提交的表单,即使我更改代码以
- 使用formvalidation.io在成功时启动引导模式
- 如果选中复选框,则仅验证子输入字段formvalidation
- 带有Select2的FormValidation未捕获异常:未为Select2定义查询函数
- jQuery formValidation在按enter键时未提交表单
- jQuery验证:未捕获类型错误:$(..)formValidation不是一个函数
- 引导formvalidation.io远程验证器MySQL PHP
- 带有日期时间选择器和格式('LLLL')的formValidation