jquery 验证插件,我如何验证一个字段但不需要它
我正在使用 jquery 验证插件进行表单的客户端验证。
我有一个字段 StandardPrice 是必需的并且需要是一个数字。下面的验证代码完美运行。
我在名为 MinPrice 的表单中还有另一个字段,该字段是可选的,但我确实想验证用户是否在其中放置了一些内容,它是一个数字。我认为通过删除 required: true 但仍然具有 number: true 它会支持这一点,但它似乎不允许我将此字段留空(即使我没有 required: true 设置)
jquery 验证插件是否支持验证字段(如果已填充),但如果字段为空则保留该字段。
这是我的代码:
$("#productForm").validate({
rules: {
StandardPrice: {
required: true,
number: true
},
MinPrice: {
number: true
}
}
});
这是字段为空时验证的屏幕截图:
这是我的html 输出(我的实际代码使用了大量 HTML.helpers() 和 Html.ValidationMessageFor()
<form action="/Product/EditProduct" id="productForm" method="post"> <fieldset>
<legend>Product</legend>
<div class="editor-label">
<label for="Product_Name">Name</label>
</div>
<div class="editor-field">
<input class="required" id="Product_Name" name="Product.Name" style="width:450px;" type="text" value="Linzer Tart" />
<span class="field-validation-valid" data-valmsg-for="Product.Name" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Product_StandardPrice">StandardPrice</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-number="The field StandardPrice must be a number." data-val-required="The StandardPrice field is required." id="Product_StandardPrice" name="Product.StandardPrice" style="width:45px;text-align:right;" type="text" value="1.50" />
<span class="field-validation-valid" data-valmsg-for="Product.StandardPrice" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Product_MiniPrice">MiniPrice</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-number="The field MiniPrice must be a number." data-val-required="The MiniPrice field is required." id="Product_MiniPrice" name="Product.MiniPrice" style="width:45px;text-align:right;" type="text" value="0.00" />
<span class="field-validation-valid" data-valmsg-for="Product.MiniPrice" data-valmsg-replace="true"></span>
</div>
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Product_Id" name="Product.Id" type="hidden" value="102" />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
I am using jquery validation plugin for clientside validation of a form.
I have a field StandardPrice which is required and needs to be a number. The validation code below works perfect.
I have another field in a form called MinPrice that is optional but I do want to validate that if a user puts something in there that it is a number. I thought by removing the required: true but still having the number: true it would support this but it seems like its not allowing me to leave this field blank (even though I don't have required: true set)
Does jquery validation plugin support validating a field IF its populated but leaving it alone if its blank.
Here is my code:
$("#productForm").validate({
rules: {
StandardPrice: {
required: true,
number: true
},
MinPrice: {
number: true
}
}
});
Here is a screenshot of the validation when the field is empty:
and here is my html output (my actual code is using a lot of HTML.helpers() and Html.ValidationMessageFor()
<form action="/Product/EditProduct" id="productForm" method="post"> <fieldset>
<legend>Product</legend>
<div class="editor-label">
<label for="Product_Name">Name</label>
</div>
<div class="editor-field">
<input class="required" id="Product_Name" name="Product.Name" style="width:450px;" type="text" value="Linzer Tart" />
<span class="field-validation-valid" data-valmsg-for="Product.Name" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Product_StandardPrice">StandardPrice</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-number="The field StandardPrice must be a number." data-val-required="The StandardPrice field is required." id="Product_StandardPrice" name="Product.StandardPrice" style="width:45px;text-align:right;" type="text" value="1.50" />
<span class="field-validation-valid" data-valmsg-for="Product.StandardPrice" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Product_MiniPrice">MiniPrice</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-number="The field MiniPrice must be a number." data-val-required="The MiniPrice field is required." id="Product_MiniPrice" name="Product.MiniPrice" style="width:45px;text-align:right;" type="text" value="0.00" />
<span class="field-validation-valid" data-valmsg-for="Product.MiniPrice" data-valmsg-replace="true"></span>
</div>
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Product_Id" name="Product.Id" type="hidden" value="102" />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
number: true
应该如您所期望的那样工作,如现场演示所示。显示标记后,您的字段名称和 jQuery 验证名称之间似乎存在一些不一致。例如,您的输入名称为
Product.StandardPrice
,而不是您在 jQuery.validate 中使用的StandardPrice
。因此,您的验证规则实际上都不会适用,因为它没有任何相应的表单元素。因此请修复它:另请注意,它应该是
Product.MiniPrice
而不是Product.MinPrice
。请保持一致。备注:查看您的标记,很明显这是由 ASP.NET MVC 3 HTML 帮助程序生成的。为什么使用自定义 jQuery.validate 规则而不是默认的不显眼的规则?
number: true
should work as you expect as seen in this live demo.After showing your markup it seems that you have some inconsistency between your field names and your jQuery validate names. For example your input is named
Product.StandardPrice
, notStandardPrice
which is what you are using in your jQuery.validate. So none of your validate rules actually will apply as it doesn't have any corresponding form element. So fix it:Also notice that it should be
Product.MiniPrice
and notProduct.MinPrice
. Please be consistent.Remark: looking at your markup it is pretty clear that this is generated by ASP.NET MVC 3 HTML helpers. Why are you using custom jQuery.validate rules instead of the default unobtrusive ones?
请提供您的 html。这对我来说效果很好:
});
Please provide your html. This works fine for me:
});