我的用户模型具有这些数据注释以验证输入字段:
[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
。
我可以在这做什么?感谢。
答案 0 :(得分:101)
如果您使用的是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);
}
}
答案 1 :(得分:0)
其他数据注释是可选的,您可以根据需要添加这些注释,但是您需要这样做才能实现密码比较操作
decimal xreqNo;
bool isDecimalValue = decimal.TryParse(this.TextBox1.Text, out xreqNo);
if (isDecimalValue)
{
cmnd.Parameters.Add("xreq_no", OleDbType.Numeric, 8).Value = xreqNo;
}
答案 2 :(得分:0)
您可以使用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; }