JQuery 1.5中断比较验证(JQuery Validate 1.8)

时间:2011-02-25 12:52:38

标签: jquery asp.net-mvc-3 jquery-validate

升级到JQuery 1.5及更高版本1.5.1后,我的比较验证失败。我正在使用JQuery.Validate 1.7。我的ViewModel具有以下数据注释:

/// <summary>
/// Gets or sets the full name.
/// </summary>
/// <value>The full name.</value>
[Required]
[Display(Name = "fullname", ResourceType = typeof(Milkshake.Commerce.Model.Resources.Text))]
public string FullName { get; set; }

/// <summary>
/// Gets or sets the email.
/// </summary>
/// <value>The email.</value>
[DataType(DataType.EmailAddress)]
[Display(Name = "email", ResourceType = typeof(Milkshake.Commerce.Model.Resources.Text))]
[Required(ErrorMessageResourceName = "EmailRequired", ErrorMessageResourceType = typeof(Milkshake.Commerce.Model.Resources.ValidationMessages))]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessageResourceType = typeof(Milkshake.Commerce.Model.Resources.ValidationMessages), ErrorMessageResourceName = "EmailInvalid")]
public string Email { get; set; }

/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
[DataType(DataType.Password)]
[Display(Name = "password", ResourceType = typeof(Milkshake.Commerce.Model.Resources.Text))]
[Required(ErrorMessageResourceName = "PasswordRequired", ErrorMessageResourceType = typeof(Milkshake.Commerce.Model.Resources.ValidationMessages))]
[ValidatePasswordLengthAttribute(ErrorMessageResourceName = "PasswordLength", ErrorMessageResourceType = typeof(Milkshake.Commerce.Model.Resources.ValidationMessages))]
public string Password { get; set; }

/// <summary>
/// Gets or sets the confirm password.
/// </summary>
/// <value>The confirm password.</value>
[DataType(DataType.Password)]
[Display(Name = "confirmPassword", ResourceType = typeof(Milkshake.Commerce.Model.Resources.Text))]
[Required(ErrorMessageResourceName = "PasswordRequired", ErrorMessageResourceType = typeof(Milkshake.Commerce.Model.Resources.ValidationMessages))]
[Compare("Password", ErrorMessageResourceName = "PasswordsMustMatch", ErrorMessageResourceType = typeof(Milkshake.Commerce.Model.Resources.ValidationMessages))]
public string ConfirmPassword { get; set; }

我输入的任何值,密码字段永远不会相同。

更新 - ASP.NET AntiForgeryToken遇到麻烦。

在FireBug设置断点中鬼混后,我注意到在equalTo验证函数中,从jquery.validate.js中的第1065行开始,找到的目标元素不是密码字段 - 而是{{1}当您使用__RequestVerificationToken帮助程序时,ASP.NET MVC会写入。

这意味着我们甚至没有比较正确的输入元素。为了解决这个问题,我在jquery.validate.js文件中添加了一个脏的黑客:

Html.AntiForgeryToken()

这个hack,获取data-val-equalto-other属性的值,并将其与自己的ID混合,以找到正确的输入元素。在所有情况下都不会起作用。但在上述情况下,对我有用。

6 个答案:

答案 0 :(得分:11)

我发现这是由于jquery.validate.unobtrusive.js中的错误

Unobtrusive中的代码添加了一个等于适配器,它尝试使用以下代码通过其name属性查找匹配元素:

element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

fullOtherName变量通常(并且可能总是)用句点命名空间,因此jQuery选择器返回太多输入,并选择第一个。这段时间需要使用.replace(“。”,“\\。”)进行转义,为您提供:

element = $(options.form).find(":input[name=" + fullOtherName.replace(".", "\\.") + "]")[0];

类似的构造存在一些较低的行,并且还需要修复(以及缩小的Javascript)。

答案 1 :(得分:2)

如果它仍然相关,我有同样的问题,升级所有脚本,现在它的工作原理。 我使用以下内容:Jquery 1.8(来自他们的cdn的正式版),Jquery验证1.9和jquery unobtrusive验证来自microsoft on nunget提供的最新软件包(奇怪的是,获得了j jquery验证1.8而不是1.9)。上帝保佑微软和使用3个脚本文件管理的混乱:\

答案 2 :(得分:1)

您可以在Microsoft CDN上使用最新版本的jquery和jquery.validate。我看不到jquery.validate.unobtrusive.min.js的更新版本。

MVC4附带的jquery.validate.unobtrusive.min.js版本运行正常。

<!-- Scripts -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
<!-- ToDo: Will Microsoft fix this on the CDN? -->
<script type="text/javascript" src="@Url.Content("~/Scripts/Frameworks/jquery.validate.unobtrusive.min.js")"></script>

答案 3 :(得分:0)

来自http://bassistance.de/jquery-plugins/jquery-plugin-validation/

  

请注意:最新版本不是,   但是,与jQuery 1.5.x兼容。您   可以找到兼容的版本   GitHub repository

但是,没有一个分支对我有用......

答案 4 :(得分:0)

为了回应MartinHN的更新,我遇到了同样的问题并应用了相同的修复程序。当我使用嵌套编辑器模板时,我的otherElementId中有多个下划线,因此更改了他的代码以使用lastIndexOf而不是indexOf。 E.g。

var underScoreIndex = otherElementId.indexOf("_");

更改为:

var underScoreIndex = otherElementId.lastIndexOf("_");

答案 5 :(得分:0)

看起来如果你看一下新的MVC4代码。你可以看到jquery.validate.unobtrusive.js的新版本: http://code.msdn.microsoft.com/ASPNET-MVC-4-Mobile-e99ed0ed/sourcecode?fileId=49443&pathId=640259357 如果你做差异。它与MVC3版本完全相同,只是上面提到的微小更改以不同的方式完成。所以你应该做的就是用代码示例中的那个替换你的jquery.validate.unobtrusive.js,所有这些都可行。

就这样你知道。我正在使用Jquery 1.6.4和Jquery mobile 1.0.1