[英]Data Annotation to validate confirm password
我的用戶模型有這些數據注釋來驗證輸入字段:
[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; }
但是,我無法確定如何確保確認密碼與密碼相同。 據我所知,只有這些驗證例程存在: Required, StringLength, Range, RegularExpression
。
我能在這里做什么? 謝謝。
如果您使用的是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; }
編輯:對於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);
}
}
您可以使用比較注釋來比較兩個值,如果您需要確保它不會在下游任何地方保留(例如,如果您使用EF)您還可以添加NotMapped以忽略任何實體 - >數據庫映射
有關可用數據注釋的完整列表,請參見此處:
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; }
其他數據注釋是可選的,您可以根據需要添加這些注釋,但您需要執行此操作以實現密碼比較操作
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]`enter code here`
[Compare("Password")]
public string ConfirmPassword { get; set; }
EditForm
驗證上述答案對我不起作用,因為它沒有顯示任何消息。
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public sealed class PropertyMustMatchAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
public PropertyMustMatchAttribute(string originalProperty)
: base(_defaultErrorMessage)
{
OriginalProperty = originalProperty;
}
public string OriginalProperty { get; }
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var properties = TypeDescriptor.GetProperties(validationContext.ObjectInstance);
var originalProperty = properties.Find(OriginalProperty, false);
var originalValue = originalProperty.GetValue(validationContext.ObjectInstance);
var confirmProperty = properties.Find(validationContext.MemberName, false);
var confirmValue = confirmProperty.GetValue(validationContext.ObjectInstance);
if (originalValue == null)
{
if (confirmValue == null)
{
return ValidationResult.Success;
}
return new ValidationResult(ErrorMessage,
new[] { validationContext.MemberName });
}
if (originalValue.Equals(confirmValue))
{
return ValidationResult.Success;
}
return new ValidationResult(ErrorMessage,
new[] { validationContext.MemberName });
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.