jquery 验证插件,我如何验证一个字段但不需要它

发布于 2024-12-02 08:45:04 字数 2820 浏览 0 评论 0原文

我正在使用 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-12-09 08:45:04

number: true 应该如您所期望的那样工作,如现场演示所示。

  • 将该字段留空 =>表单提交
  • 在字段中输入有效数字 =>表单提交
  • 输入无效数字=>显示错误消息并且表单未提交

显示标记后,您的字段名称和 jQuery 验证名称之间似乎存在一些不一致。例如,您的输入名称为 Product.StandardPrice,而不是您在 jQuery.validate 中使用的 StandardPrice。因此,您的验证规则实际上都不会适用,因为它没有任何相应的表单元素。因此请修复它:

$("#productForm").validate({
     rules: {
         'Product.StandardPrice': {
             required: true,
             number: true
         },
         'Product.MiniPrice': {
             number: true
         }
     }
});

另请注意,它应该是 Product.MiniPrice 而不是 Product.MinPrice。请保持一致。

备注:查看您的标记,很明显这是由 ASP.NET MVC 3 HTML 帮助程序生成的。为什么使用自定义 jQuery.validate 规则而不是默认的不显眼的规则?

number: true should work as you expect as seen in this live demo.

  • leave the field blank => the form submits
  • enter a valid number in the field => the form submits
  • enter an invalid number => an error message is shown and the form doesn't submit

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, not StandardPrice 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:

$("#productForm").validate({
     rules: {
         'Product.StandardPrice': {
             required: true,
             number: true
         },
         'Product.MiniPrice': {
             number: true
         }
     }
});

Also notice that it should be Product.MiniPrice and not Product.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?

岁月静好 2024-12-09 08:45:04

请提供您的 html。这对我来说效果很好:

<input id="StandardPrice" name="StandardPrice" type="textbox" />
<input id="MinPrice" name="MinPrice" type="textbox" />

$("#productForm").validate({
 rules: {
     StandardPrice: {
         required: true,
         number: true
     },
     MinPrice: {
         number: true
     }
 }

});

Please provide your html. This works fine for me:

<input id="StandardPrice" name="StandardPrice" type="textbox" />
<input id="MinPrice" name="MinPrice" type="textbox" />

$("#productForm").validate({
 rules: {
     StandardPrice: {
         required: true,
         number: true
     },
     MinPrice: {
         number: true
     }
 }

});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文