问题描述
我正在使用MVC3进行无干扰的验证.我有一个字段,希望用户填充一些数据,然后按搜索"按钮.如果从未按下过搜索,或者用户在按下搜索后更改了输入字段,则该表格将无法提交.
I'm using MVC3 with unobtrusive validation. I have a field that the user is expected to fill with some data and then press a "search" button. If search has never been pressed or the user has changed the input field after pressing search, the form should not be possible to submit.
我添加了一个隐藏字段,该隐藏字段由按钮的click()
事件设置为true
,并由输入框的keyup()
事件清空.现在,我想添加一个验证规则,该规则要求隐藏字段为true
才能提交.
I've added a hidden field that is set to true
by the click()
event of the button and emptied by the keyup()
event of the input box. Now I would like to add a validation rule that requires the hidden field to be true
to allow submit.
优选地,我想使用非侵入式验证,但如果不起作用,则可以使用需要一些javascript的东西,只要它不会破坏表单其余部分的非侵入式验证即可.
Preferably I would like to use unobtrusive validation, but if that doesn't work it is ok with something that requires some javascript, as long as it doesn't spoil the unobtrusive validation for the rest of the form.
在添加type="hidden"
之前,以下代码片段完全可以满足我的要求.
The following code snippet does exactly what I want, until I add type="hidden"
.
<input class="required" id="client-searched" data-val="true"
name="ClientSearched" data-val-required="Press search!"/>
<span class="field-validation-valid" data-valmsg-replace="true"
data-valmsg-for="ClientSearched"/>
推荐答案
尝试
var validator = $("#myFormId").data('validator');
validator.settings.ignore = "";
这是内容丰富的 博客文章
Here is an informative blog post
编辑
@RAM建议了更好的解决方案,请关注
@RAM suggested a better solution please FOLLOW
这篇关于验证隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!