升級到JQuery 1.5和更高版本1.5.1後,我的比較驗證失敗。我正在使用JQuery.Validate 1.7。我的視圖模型具有以下數據註釋:JQuery 1.5中斷比較驗證(JQuery驗證1.8)
/// <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,即發現目標元素,是不是密碼字段 - 但__RequestVerificationToken
那當您使用Html.AntiForgeryToken()
幫手時,ASP.NET MVC會寫入。
這意味着我們甚至沒有比較正確的輸入元素。要解決這個問題,我添加了一個骯髒的黑客到jquery.validate.js文件:
// http://docs.jquery.com/Plugins/Validation/Methods/equalTo
equalTo: function (value, element, param) {
// bind to the blur event of the target in order to revalidate whenever the target field is updated
// TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
var target = $(param).unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
$(element).valid();
});
if ($(target).is("input[type=hidden]") && $(target).attr("name") == "__RequestVerificationToken") {
var otherElementId = $(element).attr("id");
var underScoreIndex = otherElementId.indexOf("_");
otherElementId = otherElementId.substring(0, underScoreIndex + 1);
otherElementId += $(element).attr("data-val-equalto-other").substring(2);
target = $("#" + otherElementId);
}
return value == target.val();
}
這個技巧,獲取數據-VAL-equalto-其他屬性的值,並用自己的ID混合它,找到正確的輸入元素。在所有情況下都不起作用。但在上述情況下適用於我。
它的工作原理!不知道這個錯誤是否已經存在於微軟的世界。我應該通過Microsoft Connect報告它,以便在下一個版本中解決這個問題。 – MartinHN
它已在代碼中修復。謝謝。 http://connect.microsoft.com/VisualStudio/feedback/details/665793/jquery-unobtrusive-validate-equalto-fails-with-compare-attribute – jsgoupil
這非常有幫助!但是,在我的情況下,我不得不將它改爲元= $(options.form).find(「:input [name ='」+ fullOtherName.replace(「。」,「\\。」)+「']」 )[0];注意注入值周圍的單引號。並且不要忘記在縮小版本中修復它! :) –