数据注释以验证确认密码

Jam*_*son 50 c# asp.net data-annotations

我的用户模型具有这些数据注释以验证输入字段:

[Required(ErrorMessage = "Username is required")]
[StringLength(16, ErrorMessage = "Must be between 3 and 16 characters", MinimumLength = 3)]
public string Username { get; set; }

[Required(ErrorMessage = "Email is required"]
[StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)]
[RegularExpression("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", ErrorMessage = "Must be a valid email")]
public string Email { get; set; }

[Required(ErrorMessage = "Password is required"]
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
public string Password { get; set; }

[Required(ErrorMessage = "Confirm Password is required"]
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
public string ConfirmPassword { get; set; }
Run Code Online (Sandbox Code Playgroud)

但是,我在确定如何确保确认密码与密码相同时遇到问题.据我所知,只有这些验证程序存在:Required, StringLength, Range, RegularExpression.

我能在这做什么?谢谢.

Pra*_*thi 105

如果您正在使用ASP.Net MVC 3,您可以使用System.Web.Mvc.CompareAttribute.

如果你正在使用ASP.Net 4.5它,它就在System.Component.DataAnnotations.

[Required(ErrorMessage = "Password is required")]
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required(ErrorMessage = "Confirm Password is required")]
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
[DataType(DataType.Password)]
[Compare("Password")]
public string ConfirmPassword { get; set; }
Run Code Online (Sandbox Code Playgroud)

编辑:MVC2使用以下逻辑,使用PropertiesMustMatch替代Compare属性[以下代码从默认MVCApplication项目模板复制.]

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public sealed class PropertiesMustMatchAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
    private readonly object _typeId = new object();

    public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
        : base(_defaultErrorMessage)
    {
        OriginalProperty = originalProperty;
        ConfirmProperty = confirmProperty;
    }

    public string ConfirmProperty { get; private set; }
    public string OriginalProperty { get; private set; }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            OriginalProperty, ConfirmProperty);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
        object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
        return Object.Equals(originalValue, confirmValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @MohamadMehdiHabibi,使用 [Compare("密码", ErrorMessage = "密码不匹配。")] (2认同)

小智 6

您可以使用 compare 注释来比较两个值,如果您需要确保它不会在下游的任何地方持久化(例如,如果您使用的是 EF),您还可以添加 NotMapped 以忽略任何实体->DB 映射

有关可用数据注释的完整列表,请参见此处:

https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required]
[DataType(DataType.Password)]
[Compare("Password")]
[NotMapped]
public string ConfirmPassword { get; set; }
Run Code Online (Sandbox Code Playgroud)