jQuery 验证选择框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4364291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery validate select box
提问by I-M-JM
I am using jQuery Validation Plugin for form validation and I am not able to validate list/select box
我正在使用 jQuery 验证插件进行表单验证,但我无法验证列表/选择框
<select id="select">
<option value="-1">Choose an option</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Now, I want to make sure that the user selects anything but "Choose an option" (which is the default one, and has value as -1). So that it won't validate if you choose the first option. How can this be done?
现在,我想确保用户选择的不是“选择一个选项”(这是默认选项,值为 -1)。这样如果您选择第一个选项,它就不会验证。如何才能做到这一点?
采纳答案by Hoàng Long
You can use Jquery Validator custom rule.
您可以使用Jquery Validator 自定义规则。
A similar example is discussed here. The code below is from the example:
这里讨论了一个类似的例子。下面的代码来自示例:
// add the rule here
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}, "Value must not equal arg.");
// configure your validation
$("form").validate({
rules: {
SelectName: { valueNotEquals: "-1" }
},
messages: {
SelectName: { valueNotEquals: "Please select an item!" }
}
});
回答by Vicky
Put <option value="">Choose an option</option>
as blank rather than -1
将其<option value="">Choose an option</option>
设为空白而不是 -1
using jquery validation
使用 jquery 验证
rules: {
select: "required"
}
回答by Vicky
Add this in your rule
将此添加到您的规则中
select:{
required: function(element) {
if( $("#select").val() =='-1'){
return false;
} else {
return true;
}
}
}
回答by Robin
Or use the element value:
或者使用元素值:
required: function(elm) {
if(elm.options[elm.options.selectedIndex].value != "-1") {
return true;
} else {
return false;
}
}
回答by Alexander Behling
Why don't you use the value of the condition?
为什么不使用条件的值?
required: function(elm) {
return (elm.options[elm.options.selectedIndex].value != "-1");
}
This does the same as the solution from Robin, but is much shorter.
这与 Robin 的解决方案相同,但要短得多。
回答by Darren
How can we validate for jquery ui select dropdown
我们如何验证 jquery ui 选择下拉列表
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>
<script>
$(".intselect").selectmenu();
$().ready(function() {
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$( "#validateregister" ).validate({
rules: {
firstname: {
required: true
},
email: {
required: true,
email: true
},
year: {
required: true
}
}
});
$('#validateregister').change(function() {
if ($("#validateregister").valid()) {
$(".go").removeAttr("disabled");
}
else{
$(".go").attr("disabled","disabled");
}
});
});
</script>
"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<form id="validateregister" style="width: 300px;">
<div> <br />
<input type="text" name="firstname" class="form-control" id="firstname" />
</div>
<div> <br />
<input type="text" name="email" class="form-control" id="email" />
</div>
<div> <br />
<select class="form-control intselect" name="year" id="year">
<option value="">Select year</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2015</option>
</select>
</div>
<div> <br />
<input type="submit" class="btn btn-default go" value="submit" disabled>
</div>
</form>