Phone 使用 DataAnnotation 进行验证
Phone validation with DataAnnotation
我想验证我的 phone 号码,对于每个无效案例,我想收到另一条错误消息:
所以,我的验证规则:
- 只有 9 位数字 -> 错误信息 = "error message 1"
- 号码不能相同,例如 222222222 > 错误
消息 = "error message 2"
- 不能是像 '123456789' 这样的字符串 > 错误消息 = "error message 3"
- 不能以 0 开头 > 错误信息 ="error message 4"
我的手机属性:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MyPhoneAttribute : RegularExpressionAttribute
{
private const string myRegex = @"^([0-9]{9}){0,1}$";
public MyPhoneAttribute () : base(myRegex )
{
ErrorMessage = "default error message";
}
public override bool IsValid(object value)
{
if (string.IsNullOrEmpty(value as string))
return true;
if (value.ToString() == "123456789")
{
return false;
}
if (Regex.IsMatch(value.ToString(), @"^([0-9])*$"))
{
return false;
}
return Regex.IsMatch(value.ToString(), @"^([0-9])*$");
}
}
这不是完美的解决方案,我稍后会对其进行改进,但现在您可以尝试类似的方法(注意,这里我使用 ValidationAttribute
而不是 RegularExpressionAttribute
作为基础 class):
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MyPhoneAttribute : ValidationAttribute
{
private const string nineDigitsPattern = @"^([0-9]{9}){0,1}$";
private const string sameNumberPattern = @"(\w){9,}"; // here 9 the same numbers
public override bool IsValid(object value)
{
var validationNumber = (string)value;
if (string.IsNullOrEmpty(validationNumber))
return true;
// case 4
if (validationNumber.StartsWith("0"))
{
ErrorMessage = "error message 4";
return false;
}
// case 3
if (validationNumber.Equals("123456789"))
{
ErrorMessage = "error message 3";
return false;
}
// case 2
if (Regex.IsMatch(validationNumber, sameNumberPattern)) {
ErrorMessage = "error message 2";
return false;
}
// case 1
if (Regex.IsMatch(validationNumber, nineDigitsPattern))
{
ErrorMessage = "error message 1";
return false;
}
return true;
}
}
我想验证我的 phone 号码,对于每个无效案例,我想收到另一条错误消息:
所以,我的验证规则:
- 只有 9 位数字 -> 错误信息 = "error message 1"
- 号码不能相同,例如 222222222 > 错误 消息 = "error message 2"
- 不能是像 '123456789' 这样的字符串 > 错误消息 = "error message 3"
- 不能以 0 开头 > 错误信息 ="error message 4"
我的手机属性:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MyPhoneAttribute : RegularExpressionAttribute
{
private const string myRegex = @"^([0-9]{9}){0,1}$";
public MyPhoneAttribute () : base(myRegex )
{
ErrorMessage = "default error message";
}
public override bool IsValid(object value)
{
if (string.IsNullOrEmpty(value as string))
return true;
if (value.ToString() == "123456789")
{
return false;
}
if (Regex.IsMatch(value.ToString(), @"^([0-9])*$"))
{
return false;
}
return Regex.IsMatch(value.ToString(), @"^([0-9])*$");
}
}
这不是完美的解决方案,我稍后会对其进行改进,但现在您可以尝试类似的方法(注意,这里我使用 ValidationAttribute
而不是 RegularExpressionAttribute
作为基础 class):
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MyPhoneAttribute : ValidationAttribute
{
private const string nineDigitsPattern = @"^([0-9]{9}){0,1}$";
private const string sameNumberPattern = @"(\w){9,}"; // here 9 the same numbers
public override bool IsValid(object value)
{
var validationNumber = (string)value;
if (string.IsNullOrEmpty(validationNumber))
return true;
// case 4
if (validationNumber.StartsWith("0"))
{
ErrorMessage = "error message 4";
return false;
}
// case 3
if (validationNumber.Equals("123456789"))
{
ErrorMessage = "error message 3";
return false;
}
// case 2
if (Regex.IsMatch(validationNumber, sameNumberPattern)) {
ErrorMessage = "error message 2";
return false;
}
// case 1
if (Regex.IsMatch(validationNumber, nineDigitsPattern))
{
ErrorMessage = "error message 1";
return false;
}
return true;
}
}