MVC3验证输入不等于属性

MVC3 Validate input not-equal attribute

本文关键字:属性 不等于 输入 验证 MVC3      更新时间:2023-09-26

我已经完全按照Darin Dimitrov在以下位置提供的方式实现了NotEqual属性:https://stackoverflow.com/a/5742494/1946707

我在这里遇到的问题是,我使用的是jQuery对话框,并且希望在对话框的客户端进行验证。我需要的验证启动,但不相等的验证在客户端不起作用。。。任何帮助都将不胜感激。

这是我的型号:

public class ChangePasswordModel
{
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Current password")]
    public string OldPassword { get; set; }
    [Required]
    [NotEqual("OldPassword", ErrorMessage = "should be different than Prop1")]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }
    [DataType(DataType.Password)]
    [Display(Name = "Confirm new password")]
    [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

这是我的观点:

@using (Html.BeginForm("ChangePassword", "Account", new { area = "" }, FormMethod.Post, new { id = "changePasswordForm" }))
{
    <a href="#" onclick="openChangePasswordDialog();">Change Password</a>
    <div id="dialogChangePassword" title="Change Password">
        <table>
            <tr>
                <td>@Html.LabelFor(m => m.OldPassword)
                </td>
                <td>@Html.PasswordFor(m => m.OldPassword)
                </td>
                <td>@Html.ValidationMessageFor(m => m.OldPassword)
                </td>
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.NewPassword)
                </td>
                <td>@Html.PasswordFor(m => m.NewPassword)
                </td>
                <td>@Html.ValidationMessageFor(m => m.NewPassword)
                </td>
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.ConfirmPassword)
                </td>
                <td>@Html.PasswordFor(m => m.ConfirmPassword)
                </td>
                <td>@Html.ValidationMessageFor(m => m.ConfirmPassword)
                </td>
            </tr>
        </table>
    </div>
}

这是我的javascript:

    var changePasswordDialogId = '#dialogChangePassword';
    var oldPasswordInput = '#changePasswordForm #OldPassword';
    var newPasswordInput = '#changePasswordForm #NewPassword';
    var confirmPasswordInput = '#changePasswordForm #ConfirmPassword';
    $(document).ready(function () {
        $(changePasswordDialogId).dialog(
    {
        autoOpen: false,
        autoResize: true,
        modal: true,
        open: function () {
            $(this).parent().appendTo("#changePasswordForm");
        },
        buttons: {
            Ok: function () {
                if ($(oldPasswordInput).valid() && $(newPasswordInput).valid() && $(confirmPasswordInput).valid()) {
                    alert('ready to post!');
                    $(changePasswordDialogId).dialog('close');
                }
            },
            Cancel: function () {
                $(changePasswordDialogId).dialog('close');
            }
        }
    });
});       //document.ready
function openChangePasswordDialog() {
    $(changePasswordDialogId).dialog('open');
}

似乎忘记注册验证适配器

jQuery.validator.unobtrusive.adapters.add(
        'notequalto', ['other'], function (options) {
            options.rules['notEqualTo'] = '#' + options.params.other;
            if (options.message) {
                options.messages['notEqualTo'] = options.message;
            }
    });

Darin Dimitrov在回答中详细说明了这一点。

好的,所以我发现了我的问题。我错过了IClientValidatable的继承现在我已经添加了它,它可以在客户端工作,甚至在我的jQuery对话框中也是如此。