jQuery MVC:如何在隐藏字段上启用客户端验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24572749/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:35:52  来源:igfitidea点击:

MVC : How to enable client side validation on hidden fields

jqueryasp.net-mvcasp.net-mvc-4jquery-validateunobtrusive-validation

提问by RollerCosta

@Scripts

@脚本

 <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")"></script>
  <script src="@Url.Content("~/Scripts/jquery.validate.js")"></script>
  <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script>

@View

@看法

@using (Html.BeginForm("TestModelState", "RandD", FormMethod.Post, new {id="form123" }))
{ 
    @Html.TextBoxFor(x => x.htmlText, new { style="display:none"})<br />
    @Html.ValidationMessageFor(x => x.htmlText)
    <div>
      @Html.TextBoxFor(x => x.Uprop1)<br />
      @Html.ValidationMessageFor(x => x.Uprop1)
    </div>
    <input type="submit" value-="Submit" onclick="abc()" />
}

WHAT I have tried

我试过的

  1. Replaced ignore: ":hidden",with ignore: "", inside validate.js
  2. var validateMeta = $('#form123').validate(); validateMeta.settings.ignore = "";

  3. $.validator.setDefaults({ignore: ""});

  4. $.validator.setDefaults({ ignore: [] });

  1. 替换ignore: ":hidden",使用ignore: "",在 validate.js 中
  2. var validateMeta = $('#form123').validate(); validateMeta.settings.ignore = "";

  3. $.validator.setDefaults({ignore: ""});

  4. $.validator.setDefaults({ ignore: [] });

回答by Robin Hames

I ran into this same problem. A hidden field on a page was storing a user ID, which was selected from an autocomplete text box. This ID had validation to ensure that a non-zero id was posted back. We wanted to include this validation in the client side.

我遇到了同样的问题。页面上的隐藏字段存储用户 ID,该 ID 是从自动完成文本框中选择的。此 ID 已验证以确保回发非零 ID。我们希望在客户端包含此验证。

By default jQuery validate ignores hidden fields, elements with zero width and height, those with css display:none and any elements with an invisible parent (using same criteria).

默认情况下,jQuery 验证会忽略隐藏字段、宽度和高度为零的元素、css display:none 和任何具有不可见父元素的元素(使用相同的标准)。

The ignore setting can however be overridden by adding the following script (I just added it to my custom validator script before calling $.validator.addMethod()):

然而,可以通过添加以下脚本来覆盖忽略设置(我只是在调用 $.validator.addMethod() 之前将其添加到我的自定义验证器脚本中):

// By default validator ignores hidden fields.
// change the setting here to ignore nothing
$.validator.setDefaults({ ignore: null });

NOTE:This code won't work if it's run inside the document readyor jQuery $(function () {})method.

注意:如果此代码在document ready或 jQuery$(function () {})方法中运行,则此代码将不起作用。

回答by IonutC

Just after you insert the script for validation set the ignore to "":

在您插入验证脚本后,将忽略设置为“”:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
    $.validator.setDefaults({
        ignore: ""
    });
    </script>
}

回答by Mr.LamYahoo

try

尝试

 var validator = $("#form-id").data('validator');
 validator.settings.ignore = "";

回答by sanjeev

I am not sure why do you need to show validations for fields that user cannot see or edit. But you can try this. Instead of display:none, apply visibility:hiddenand height:0to your input field.

我不确定您为什么需要显示用户无法看到或编辑的字段的验证。但是你可以试试这个。而不是display:none,将 visibility:hiddenheight:0应用于您的输入字段。

@using (Html.BeginForm("TestModelState", "RandD", FormMethod.Post, new {id="form123" }))
{ 
    @Html.TextBoxFor(x => x.htmlText, new { style="visibility:hidden;height:0"})<br />
    @Html.ValidationMessageFor(x => x.htmlText)
    <div>
      @Html.TextBoxFor(x => x.Uprop1)<br />
      @Html.ValidationMessageFor(x => x.Uprop1)
    </div>
    <input type="submit" value-="Submit" onclick="abc()" />
}