ASP.NET MVC 2 - 多个正则表达式上的属性 [英] ASP.NET MVC 2 - Multiple Regex on a property
本文介绍了ASP.NET MVC 2 - 多个正则表达式上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,如果在asp.net MVC 2存在方式,对亲preTY多个正前pression。例如:
I would like to know if a way exists in asp.net mvc 2, to have multiple regular expression on a proprety. For example :
[RegularExpression("[^0-9]", ErrorMessageResourceName = ValidationMessageResourceNames.OnlyDigits, ErrorMessageResourceType = typeof(ValidationMessages))]
[RegularExpression("[^<>]{2,}", ErrorMessageResourceName = ValidationMessageResourceNames.SpecialCharErrorCreateAccount, ErrorMessageResourceType = typeof(ValidationMessages))]
public string City { get; set; }
这里的目标,在两个有两个特定的错误消息,一个用于数字和一个其它的特殊字符和事实的分lenght必须是2个字符。
The target here, is two have two specific error messages, one for the digits and one other for the special Chars and the fact that the min lenght must be 2 chars.
在此先感谢您的帮助,或经验。
Thanks in advance for the help, or experience.
艾蒂安。
推荐答案
事情是这样的:
public class DigitsAttribute : RegularExpressionAttribute
{
public DigitsAttribute()
: base("[^0-9]")
{
}
}
public class SpecialCharsAttribute : RegularExpressionAttribute
{
public SpecialCharsAttribute()
: base("[^<>]{2,}")
{
}
}
使用:
[Digits]
[SpecialChars]
public string City { get; set; }
这篇关于ASP.NET MVC 2 - 多个正则表达式上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文