自DataAnnotation在asp.net mvc的不工作
问题描述:
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[Minimumthreenumbers]
public string Password { get; set; }
public class MinimumthreenumbersAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "There should be minimum three letters in the string";
private string Otherpassword;
public MinimumthreenumbersAttribute() : base(_defaultErrorMessage)
{
}
public override bool IsValid(object value)
{
string i = value.ToString();
string jobId = i;
int digitsCount = 0;
foreach (char c in jobId)
{
if (Char.IsDigit(c))
digitsCount++;
}
if (digitsCount > 3)
{
return true;
}
else
{
return false;
}
}
}
以上是自定义属性的实现类。上面code实际上已经验证密码检查最少3 numbers.If用户输入的密码有不超过3位,则必须抛出错误
。这是规定。但没有按预期工作。如何使上述code工作任何想法?我曾尝试了一段时间,但仍然是行不通的。
The above is the custom attribute implementation class .The above code actually has to validate the password to check for minimum 3 numbers.If the user entered password has less than 3 digits it should throw an error
.This is the requirement. But is not working as expected. Any ideas on how to make the above code working? I have tried for some time but still it is not working.
答
不知道这将是足以让你code工作,但至少这将简化您的code。
Not sure that will be enough to make your code "work", but at least this will simplify your code.
public class MinimumthreenumbersAttribute : ValidationAttribute
{
public MinimumthreenumbersAttribute() : base("There should be minimum three letters in the string")
{
}
public override bool IsValid(object value)
{
return value != null &&
value.ToString()
.Where(Char.IsDigit)
.Count() >=3
}
}