我有一个非常奇怪的问题,我写了一个自定义客户端验证,现在似乎跨度[data-valmsg-for ="密码"]一直空白,它只在我尝试后填充提交表格,并在我输入(试图提交表格后),但一旦我去另一个领域,消息就会消失。
$.validator.addMethod("passwordvalidation", function (value, element, param) {
console.log("false");
var msg = $('span[data-valmsg-for="Password"]').text();
console.log(msg);
return false;
},"");
$.validator.unobtrusive.adapters.add("passwordvalidation", ["firstName", "lastName"], function (options) {
console.log("adapters");
var params = {
firstName: options.params.firstName,
lastName: options.params.lastName,
};
options.rules["passwordvalidation"] = params;
if (options.message) {
console.log(options.message);
options.messages["passwordvalidation"] = options.message;
//options.messages["passwordvalidation"] = options.message;
}
});
使用调试器查看屏幕截图: not showing the error although validation return false
[尝试提交错误后,只要我在框中显示错误] [2]
[错误再次消失] [3]
[2] http://i.stack.imgur.com/2bhnz.png
型号:
[CustomPasswordValidator(FirstName = "FirstName", LastName = "LastName")]
public string Password { get; set; }
知道为什么会这样吗?有时会显示错误消息,我不明白为什么它会变空。
感谢,
伊詹
答案 0 :(得分:0)
您可以删除验证然后重新验证您的表单,请尝试使用此
var form = $('YourForm')
.removeData("validator")
.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);