2014-02-15 24 views
0

我已经创建了一个MobileNumberAnnotation类,它具有指定正则表达式的属性。现在,当我指定RegularExpressionAttribute时,我如何告诉它将该属性的值作为模式获取?RegularExpression属性 - 传入一个类的属性字段

public class MobileNumberAnnotation 
{ 
    public string MobileFormat = "^(07(\\d ?){9})"; 
} 

我试着做了以下,但我不知道为什么它不工作,因为它期待一个字符串模式。

[Required] 
    [RegularExpressionAttribute(MobileNumberAnnotation.MobileFormat)] 
    public int MobileNumber { get; set; } 

回答

1

你可以这样做:如果你改变你的MobileFormat声明是一个const

[RegularExpression("^(07(\\d ?){9})", ErrorMessage = "Invalid Phone Number")] 
+0

*假设你正确输入你的正则表达式。 –

+0

我知道你可以做到这一点。不过谢谢你的帮助 – user1781232

0

,这样就可以解决问题

public const string MobileFormat = "^(07(\\d ?){9})";