对Asp.net-MVC中特定隐藏字段的客户端验证 [英] Client Side validation for a particular hidden field in Asp.net-MVC

查看:70
本文介绍了对Asp.net-MVC中特定隐藏字段的客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据注释进行验证,还使用unobtrusive.js进行客户端验证.

I am using data annotation for validation and also using unobtrusive.js for client side validation.

根据jquery.validate.js的默认设置(忽略::hidden"),默认情况下,我有几个未验证(并且不需要)的隐藏字段.

I have several hidden fields that are not validated (and I don't need) by default as per the default setting of jquery.validate.js (ignore: ":hidden").

就我而言,我只想验证一个特定的隐藏字段,而不是全部,因为我无法覆盖

In my case I just want to validate a particular hidden field and not the all as a result I can't override

                   ***ignore: ":hidden"*** to  ***ignore:[]***.

如果更改,则将考虑所有隐藏字段进行验证,这不是我的要求.

If it is changed then all hidden fields will be considered for validation which is not my requirement.

不知道如何为特定的隐藏字段启用验证.

Have no idea how to enable validation for a particular hidden field.

 [ValidDate(ErrorMessage = "This is not a valid date.Please enter date in dd/mm/yyyy format.")]
 public string HiddenSelectedDob { get; set; }

ValidDate是自定义属性.

在这里,我从三个不同的下拉菜单中分别输入用户的日,月和年,对于每个下拉菜单的更改事件,将使用jQuery设置HiddenSelectedDob的值.

Here I am taking input from the user for the day , month and year from three different dropdowns and for the change event of each drop down the value of the HiddenSelectedDob will be set using jQuery.

最后在提交此隐藏文件时,应先进行验证.

Finally on submit this hidden filed should be validate.

我给这个隐藏的文件命名了一个类名.

I have given a class name to that hidden filed like..

@Html.HiddenFor(m => m.HiddenSelectedDob, new { @class = "validateAlways" })

并覆盖

$.validator.setDefaults({ ignore: ":hidden:not(.validateAlways)" });

但是结果是一样的.该字段无效.

but the result is same. The field is not validating.

推荐答案

好吧,我看到的唯一方法是将一个类添加到要验证的隐藏输入中.

Well, the only way I see is to add a class to the hidden input that you wanna validate.

因此,您必须在视图级别执行此操作.

So you'll have to do this at the view level.

@Html.HiddenFor(x => x.HiddenSelectedDob, new{@class="validateAlways"})

然后,将validate.js的忽略规则更改为

Then, change the ignore rule of validate.js to

ignore: ':hidden:not('.validateAlways')'

这意味着:忽略所有隐藏的输入,除非它们具有validateAlways类.

Which means : ignore all hidden inputs, except if they have a validateAlways class.

这篇关于对Asp.net-MVC中特定隐藏字段的客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆