数据注释扩展等于和比较不起作用

时间:2013-05-20 22:28:12

标签: asp.net-mvc-3 validation compare data-annotations

我正在使用MVC3和razaor视图引擎

我正在尝试确保密码和确认密码应该匹配,但这对我不起作用。它只是告诉我“密码”和“确认密码”不匹配。

我使用了“compare”和“equalto”,两者都导致了同样的错误。

商业实体:

namespace Project.BusinessEntities
{
    public partial class RegisterPasswordUpdate
    {
        public string Password { get; set; }
        public string ConfirmPassword { get; set; }
        public string UserId { get; set; }
    }
}

验证:

using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
using System.Web.Mvc;
using Project.Resources;


namespace Project.BusinessEntities
{
    [MetadataType(typeof(RegisterPasswordUpdate.RegisterPasswordUpdateMetaData))]
    public partial class RegisterPasswordUpdate
    {
        public class RegisterPasswordUpdateMetaData
        {
            [Required(ErrorMessage = ValidationMessageConstants.ResponseRequired)]
            [StringLength(16, MinimumLength = 8)]
            [RegularExpression(@"[A-Za-z0-9]*", ErrorMessage = ValidationMessageConstants.AlphaNumericOnly)]
            public string Password { get; set; }

            [Required(ErrorMessage = ValidationMessageConstants.ResponseRequired)]
            [StringLength(16, MinimumLength = 8, ErrorMessage = ValidationMessageConstants.MinimumLength)]
            [RegularExpression(@"[A-Za-z0-9]*", ErrorMessage = ValidationMessageConstants.AlphaNumericOnly)]
            [EqualTo("Password", ErrorMessage = ValidationMessageConstants.ConfirmPassword)]
            public string ConfirmPassword { get; set; }


        }
    }
}

使用:

@Html.TextBoxFor(x => x.PasswordUpdate.Password, new { maxlength = "16", size = "16" })
@Html.ValidationMessageFor(x => x.PasswordUpdate.Password)

@Html.TextBoxFor(x => x.PasswordUpdate.ConfirmPassword, new { maxlength = "16", size = "16" })
@Html.ValidationMessageFor(x => x.PasswordUpdate.ConfirmPassword)

HTML:

<input data-val="true" data-val-length="The field Password must be a string with a minimum length of 8 and a maximum length of 16." data-val-length-max="16" data-val-length-min="8" data-val-regex="Only alphanumeric (A-Z a-z 0-9) values are allowed" data-val-regex-pattern="[A-Za-z0-9]*" data-val-required="Response required!" id="PasswordUpdate_Password" maxlength="16" name="PasswordUpdate.Password" size="16" type="text" value="" />

<span class="field-validation-valid" data-valmsg-for="PasswordUpdate.Password" data-valmsg-replace="true"></span>

<input data-val="true" data-val-equalto="Password and confirm password do not match" data-val-equalto-other="*.Password" data-val-length="Minimum length not met" data-val-length-max="16" data-val-length-min="8" data-val-regex="Only alphanumeric (A-Z a-z 0-9) values are allowed" data-val-regex-pattern="[A-Za-z0-9]*" data-val-required="Response required!" id="PasswordUpdate_ConfirmPassword" maxlength="16" name="PasswordUpdate.ConfirmPassword" size="16" type="text" value="" />

<span class="field-validation-valid" data-valmsg-for="PasswordUpdate.ConfirmPassword" data-valmsg-replace="true"></span>

页面上的html ID:

PasswordUpdate_Password PasswordUpdate_ConfirmPassword

页面上的html名称:

PasswordUpdate.Password PasswordUpdate.ConfirmPassword

这里缺少什么?

1 个答案:

答案 0 :(得分:1)

这是一个错误,以下帖子帮助解决了这个问题: http://forums.asp.net/t/1716181.aspx/1

问题在于缺少不引人注意的验证码中的单引号。这一行不正确:

 element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

应该替换为

element = $(options.form).find(":input[name='" + fullOtherName + "']")[0];