2

I'm trying to figure out how to validate that a user has entered matching passwords when they sign up. Is there anything built in to MVC 4 Data Annotations that I can use for this or is the only route creating a custom validation attribute?

If I do have to create a custom validation attribute, how do I access the password property (assuming I put the annotation on the confirm password property)? Also, are there any commonly used libraries for this type of validation?

This is what I have for the beginning of a custom validation attribute, just not sure how to access the password property:

public class CrossFieldValidationAttribute : ValidationAttribute
{
    public override bool IsValid(object value) //how do I get the other value in here?
    {
        //validation logic here
        return base.IsValid(value);
    }
}

I appreciate any help!

4

4 回答 4

10

mvc 中已经内置了一个比较验证属性。请参阅此处的文档:

http://msdn.microsoft.com/en-us/library/system.web.mvc.compareattribute(v=vs.98).aspx

一个使用示例是:

    public string Password { get; set; }

    [Compare("Password", ErrorMessage = "Uh oh")]
    public string PasswordAgain { get; set; }
于 2013-03-17T20:05:20.287 回答
2

您可以创建自定义属性并为其公共属性设置附加信息。

public class CustomValidationAttribute : ValidationAttribute
{
    public string MeaningfulValidationInfo { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // do whatever meaningful with MeaningfulValidationInfo 
        return base.IsValid(value, validationContext);
    }
}

您可以这样设置附加信息:

[CustomValidationAttribute(MeaningfulValidationInfo = "blah")]
public ActionResult Index()
{
    return View();
}

如果您尝试检查两个输入的密码是否相同,您只需在模型中验证即可。

    public class LoginModel
    {
        [Required]
        [EmailAddress]
        public string EmailAddress { get; set; }
        [Required]
        public string Password { get; set; }
        [Required]
        [Compare("Password")]
        [Display(Name = "Confirm password")]
        public string ConfirmPassword { get; set; }
    }
}
于 2013-03-17T20:11:39.663 回答
1

比较注释是最简单的选择。如下所示,Compare指向Password属性。

[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; }
于 2014-04-24T02:57:49.943 回答
-2

您可以使用ASP.NET 工具中内置的 *比较验证 *控件

我在下面提供了一个示例

<body>
    <form id="form1" runat="server">
    <div>

    <asp:Label
        id="lblBirthDate"
        Text="Birth Date:"
        AssociatedControlID="txtBirthDate"
        Runat="server" />
    <asp:TextBox
        id="txtBirthDate"
        Runat="server" />
    <asp:CompareValidator
        id="cmpBirthDate"
        Text="(Invalid Date)"
        ControlToValidate="txtBirthDate"
        Type="Date"
        Operator="DataTypeCheck"
        Runat="server" />

    <br /><br />

    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />

    </div>
    </form>
</body>

请参考以下任何链接以扩展您的知识

http://www.java2s.com/Tutorial/ASP.NET/0160__Validation/CompareValidatorperformsthreedifferenttypesofvalidations.htm

http://www.vkinfotek.com/aspnetvalidationcontrols.html

于 2013-03-17T20:05:43.553 回答