MVC 3客户端验证比较 [英] MVC 3 Client-Side Compare Validation

查看:79
本文介绍了MVC 3客户端验证比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的东西在这里发生的事情。

Something strange is happening here.

我有一个基本形式:

    <% using (Html.BeginForm())
       { %>
       <%: Html.LabelFor(model => model.user.email) %>
       <%: Html.TextBoxFor(model => model.user.email) %>
       <%: Html.ValidationMessageFor(model => model.user.email) %>
       <br />
       <%: Html.LabelFor(model => model.user.password) %>
       <%: Html.PasswordFor(model => model.user.password) %>
       <%: Html.ValidationMessageFor(model => model.user.password) %>
       <br />
       <%: Html.LabelFor(model => model.user.confirmPassword) %>
       <%: Html.PasswordFor(model => model.user.confirmPassword) %>
       <%: Html.ValidationMessageFor(model => model.user.confirmPassword) %>
       <br />
       <input type="submit" value="Submit" />
    <% } %>

头部有以下内容:

The head has the following:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<link rel="Stylesheet" type="text/css" href="/Content/Site.css" />
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

视图模型是这样的:

The ViewModel is this:

public class RegisterViewModel
{
    public RegisterUser user { get; set; }
}

public class RegisterUser
{
    [Required]
    [Display(Name = "Email Address")]
    public string email { get; set; }

    [Required]
    [Display(Name = "Password")]
    public string password { get; set; }

    [Required]
    [Display(Name = "Confirm Password")]
    [Compare("password")]
    public string confirmPassword { get; set; }
}

密码和confirmPassword之间的比较验证总是说:

确认密码和密码不匹配。

甚至当我知道他们不匹配

The compare validation between password and confirmPassword always says:
'Confirm Password' and 'password' do not match.
even when I know that they do match

下面是奇怪的一部分:当我从视图页面中删除第一个字段,一切正常。

Here's the strange part: when I remove the first field from the view page, everything works.

所以,当它只是

<% using (Html.BeginForm())
   { %>
   <%: Html.LabelFor(model => model.user.password) %>
   <%: Html.PasswordFor(model => model.user.password) %>
   <%: Html.ValidationMessageFor(model => model.user.password) %>
   <br />
   <%: Html.LabelFor(model => model.user.confirmPassword) %>
   <%: Html.PasswordFor(model => model.user.confirmPassword) %>
   <%: Html.ValidationMessageFor(model => model.user.confirmPassword) %>
   <br />
   <input type="submit" value="Submit" />
<% } %>

这一切完美的作品。

it all works perfectly.

任何想法?

感谢。

推荐答案

这是在客户端验证脚本错误:jquery.validate.unobtrusive.js

This is a bug in the client side validation script: jquery.validate.unobtrusive.js

目前行〜284,你会发现这样的:

At Line ~284 you'll find this:

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

改成这样:

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

name属性需要单引号。

The name attribute needs single quotes.

这篇关于MVC 3客户端验证比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆