自定义数据验证无效

时间:2015-09-04 10:36:37

标签: jquery html5 asp.net-mvc-5.2

我需要在密码字段上调用ajax方法进行自定义数据验证。这是我的代码 -

.cshtml

<div class="form-group" style="margin-top: 20px">
    @Html.LabelFor(m => m.Password):    
    @Html.PasswordFor(m => m.Password,
                      new
                          {
                              autofocus = "autofocus",
                              autocomplete = "off",
                              @class = "form-control",
                              value = "",
                              data_val_newpassword = "Password does not meet requirements"
                          })
    @Html.ValidationMessageFor(m => m.Password)
</div>

的.js

$.validator.addMethod("newpassword", function (value, element) {
    var isValid = false;

    $.ajax({
        url: ccm.resolveUrl('/Home/ValidateNewPassword'),
        data: { password: value },
        async: false
    }).done(function (data) {
        isValid = data.Success;
    });
    return isValid;
});

$.validator.unobtrusive.adapters.addSingleVal("newpassword");

HomeController.cs

public JsonResult ValidateNewPassword(string password)
{
    // Validate password against all password requirements.
    var isValid = _passwordValidationService.IsValidPassword(password);

    return Json(new { Success = isValid }, JsonRequestBehavior.AllowGet);
}

但是,不调用jquery方法,因此不会触发ajax请求。

请指导。

谢谢!

0 个答案:

没有答案