如何验证<select>这是一个动态数组?像这样:
<select class="escolhaVidro id_material" name="id_material[]" id="id_material">
Run Code Online (Sandbox Code Playgroud)
澄清:这是<select>由PHP动态组装的,所以我不知道索引是什么,所以这种方式是不可能的:
$(this).validate({
rules: {
"id_material[index]" : {
required : true
}
}
});
Run Code Online (Sandbox Code Playgroud)
奇怪的是这个插件不起作用:
$(this).validate({
rules: {
id_material : {
required : true
}
}
});
Run Code Online (Sandbox Code Playgroud)
我还试图用一个CSS class required中<select>,但没有好.
我需要为表单验证失败的任何情况添加句柄.
我已经读过这个,这解释了我必须添加一个处理程序,如下所示:
$('form').bind('invalid-form.validate', function () {
console.log('form is invalid!');
});
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试提交表单时,此事件才会触发.
我需要处理一个事件,该事件在表单经过后验证时被触发(即元素失去焦点等).
我想要实现的是,我有一个大型(~50个字段),并且它在Bootstrap选项卡中分割.
我想,当有任何新的验证失败或成功时,在包含无效/有效元素的选项卡的选项卡标题中设置或清除错误类.PS我的问题不在于如何在选项卡中设置这些类.我只想知道要处理哪些事件,以便在整个表单中的每个验证状态更改时得到通知.
我正在使用 jquery 和 jquery 验证。我还使用打字稿和适当的类型文件来配合它,但是当我执行这行代码时,我收到错误
$('#form').validate().settings.ignore = [];
Run Code Online (Sandbox Code Playgroud)
错误 TS2322 (TS) 类型“undefined[]”不可分配给类型“string”。
这段代码似乎是有效的并且确实有效。也许它已经改变为其他东西,但我还没有找到任何其他可行的东西。
当我在MVC Razor视图中使用Kendo UI ComboBox和DropDownList控件时,客户端验证不会触发.这是一个例子:
@using Kendo.Mvc.UI
@model KendoDropDownTest.Models.TestModel
@{
ViewBag.Title = "Kendo Drop Down and Combo Box Test";
}
<h2>Kendo Drop Down and Combo Box Test</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<div>
@Html.LabelFor(x => x.DropDownValue)
@(Html.DropDownListFor(x => x.DropDownValue, Model.Options, "-- Select an Option --"))
@Html.ValidationMessageFor(x => x.DropDownValue)
</div>
<fieldset>
<legend>Kendo</legend>
<div>
@Html.LabelFor(x => x.KendoComboValue)
@(Html.Kendo().ComboBoxFor(x => x.KendoComboValue)
.BindTo(Model.Options.Select(x => x.Text)))
@Html.ValidationMessageFor(x => x.KendoComboValue)
</div>
<div>
@Html.LabelFor(x => x.KendoDropDownValue)
@(Html.Kendo().DropDownListFor(x => x.KendoDropDownValue)
.OptionLabel("-- Select an Option --")
.BindTo(Model.Options))
@Html.ValidationMessageFor(x => x.KendoDropDownValue)
</div> …Run Code Online (Sandbox Code Playgroud) 作为这个问题的必然结果,我如何配置jQuery Validate来验证表单中的一些隐藏字段,但不是全部?
jQuery Validate 默认忽略隐藏字段,这在ASP.NET项目中是一个问题.例如,一个页面可能有一个带有复选框助手(生成隐藏inputs)的表单,以及可能利用许多隐藏输入的自定义下拉列表.
$(document).ready(function() {
$('#myform').validate({
ignore: [],
// any other options and/or rules
});
});
Run Code Online (Sandbox Code Playgroud)
像这样使用配置对象,有什么方法可以让我设置它以允许我选择要验证的特定隐藏输入,而不必指定插件必须忽略的每个单独输入?
或者,是否存在我可以在此方案中使用的验证事件挂钩?
我需要验证隐藏的字段,所以我在下面有这个(验证隐藏的字段)
$.validator.setDefaults({ ignore: [] });
Run Code Online (Sandbox Code Playgroud)
我也想忽略某个类,但我不确定两者的语法.
$.validator.setDefaults({ ignore: "[], .ignoreThisClass" });
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但我如何指定验证隐藏和忽略我的类?
我正在尝试使用jquery验证根据需要创建一个类型为多个的选择框,但它不起作用.这是我的代码
$(document).ready(function(){
$('#test').chosen();
$('#testForm').validate();
$('#validateIt').click(function(){
if($('#testForm').valid())
alert('Valid');
else
alert('Invalid');
});
});
Run Code Online (Sandbox Code Playgroud)
HTML
<form id="testForm">
<select required style="width: 165px;" id="test" name="test" class="chzn-select multiselect" multiple="multiple">
<optgroup label="Lab1"><option value="2">Some Program</option></optgroup>
</select>
<input type="button" id="validateIt" value="Run"/>
</form>
Run Code Online (Sandbox Code Playgroud)
jsfiddle:http://jsfiddle.net/jm8Dd/
我正在使用jQuery Validation插件,我正在添加自己的方法,如下所示:
jQuery.validator.addMethod("num", function(num) {
if(isNaN(num))
return false;
return true;
}, "Please enter a valid number");
Run Code Online (Sandbox Code Playgroud)
但是,即使我没有将required类添加到表单元素,如果用户没有输入任何内容,仍将调用此验证方法并将其评估为false.但我希望行为是,如果一个元素没有required该类,只有在用户输入了某些内容时才会调用它.反正有没有这样做?谢谢!
UPDATE
所以我上面发布的实际代码是一个简单的示例来演示问题,但是现在我提出了给我提出问题的具体示例.这是jsfiddle链接:
HTML
<form method="post">
<li>
<label for="2">Mileage</label>
<input type="text" name="2" id="attr_2" class="non_neg_int"
value="" />
</li>
<li>
<label for="3">VIN (Vehicle Identification Number)</label>
<input type="text" name="3"
id="attr_3" class="vin" maxlength="17" value="" />
</li>
<li>
<input type="submit" />
</li>
</form>
Run Code Online (Sandbox Code Playgroud)
JS
$(document).ready(function () {
var validator = $("form").validate({
onkeyup: false,
onblur: true,
ignore: "",
submitHandler: function (form) { // …Run Code Online (Sandbox Code Playgroud)