I have created a class MobileNumberAnnotation that has a property which specifies the regex expression. Now that when I specify the RegularExpressionAttribute how do I tell it to get the value of this property as the pattern?
public class MobileNumberAnnotation
{
public string MobileFormat = "^(07(\\d ?){9})";
}
I tried doing the following but I don't know why it doesn't work as it is expecting a a string pattern.
[Required]
[RegularExpressionAttribute(MobileNumberAnnotation.MobileFormat)]
public int MobileNumber { get; set; }
You can do something like:
[RegularExpression("^(07(\\d ?){9})", ErrorMessage = "Invalid Phone Number")]
If you change your declaration of MobileFormat
to be a const, this should fix the problem
public const string MobileFormat = "^(07(\\d ?){9})";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With