有没有办法通过 MVC 中的客户端验证执行“数据库”检查?
我有以下课程
public class EmailCheck : ValidationAttribute,IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string sErrorMessage = "Email already exists";
return new ValidationResult(sErrorMessage);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule validationRule = new ModelClientValidationRule();
validationRule.ValidationType = "checkemail";
validationRule.ErrorMessage = "Invalid Email Format";
validationRule.ValidationParameters.Add("param", "");
return new List<ModelClientValidationRule> { validationRule };
}
}
我希望客户端验证也调用“IsValid”方法 onkeyup/lost focus,而不仅仅是在“checkemail”javascript 函数中进行常规 javascript 检查。
我拥有的javascript函数如下:
//Validation for Well-Formed Email
jQuery.validator.addMethod("checkemail",
function (value, element, param) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if (!emailReg.test(value))
return false;
return true;
});
jQuery.validator.unobtrusive.adapters.add("checkemail", ["param"], function (options) {
options.rules["checkemail"] = options.params.param;
options.messages["checkemail"] = options.message;
});
如果有人能指导我正确的方向或提供类似的教程,我将不胜感激。
谢谢