即使密码相同,比较验证器也始终触发

时间:2011-08-17 21:58:32

标签: asp.net-mvc-3 unobtrusive-javascript

这就是原因。这是生成的HTML:

    <div>
        <label for="RegisterModel_Password">Contrase&#241;a</label>
        <input class="text-box single-line password" data-val="true" data-val-length="Su Contrase&amp;#241;a debe tener al menos 6 caracteres." data-val-length-max="100" data-val-length-min="6" data-val-required="Debe escribir su contrase&amp;#241;a" id="RegisterModel_Password" name="RegisterModel.Password" type="password" value="" />
        <span class="field-validation-valid" data-valmsg-for="RegisterModel.Password" data-valmsg-replace="true"></span>

    </div>

    <div>
        <label for="RegisterModel_ConfirmPassword">Confirme Su Contrase&#241;a</label>
        <input class="text-box single-line password" data-val="true" data-val-equalto="Sus contrase&amp;#241;as no son las mismas." data-val-equalto-other="*.Password" id="RegisterModel_ConfirmPassword" name="RegisterModel.ConfirmPassword" type="password" value="" />
        <span class="field-validation-valid" data-valmsg-for="RegisterModel.ConfirmPassword" data-valmsg-replace="true"></span>
    </div>

请注意,在确认密码框中:

data-val-equalto-other="*.Password"

这应该是RegisterModel.Password,因为我猜测javascript看起来像名为“RegisterModel.Password”的输入,不是吗?

这是我的型号代码:

[Required(ErrorMessage = "Debe escribir su contraseña")]
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Contraseña")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirme Su Contraseña")]
[Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")]
public string ConfirmPassword { get; set; }

有关为何发生这种情况的任何想法?


刚刚创建了一个全新的MVC3应用程序,这就是模型:

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

验证工作正常。这里唯一的区别是他们直接在视图中使用模型,而我正在传递包含该RegisterModel的* View *模型。

此工作默认值的HTML与我的不同之处在于:

        <div class="editor-field">
            <input data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="The Password field is required." id="Password" name="Password" type="password" />
            <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="ConfirmPassword">Confirm password</label>
        </div>
        <div class="editor-field">
            <input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="ConfirmPassword" name="ConfirmPassword" type="password" />
            <span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>
        </div>

2 个答案:

答案 0 :(得分:2)

您好我遇到的问题与您相同。发生这种情况是因为javascript文件中存在执行验证的错误。这是你必须做的修复。

http://forums.asp.net/t/1716181.aspx/1

在最小化版本中找到并不难,这是我必须改变的。

Saludos!

答案 1 :(得分:0)

而不是使用

@Html.PasswordFor(m => m.RegisterModel.RegisterConfirmPassword)

输出

   <input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="RegisterModel.RegisterPassword" id="RegisterModel_RegisterConfirmPassword" name="RegisterModel.RegisterConfirmPassword" type="password">

我取了上面的输出并在“'RegisterModel.RegisterPassword'”

周围添加了单引号
    <input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="'RegisterModel.RegisterPassword'" id="RegisterModel_RegisterConfirmPassword" name="RegisterModel.RegisterConfirmPassword" type="password">