Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegularExpression Attribute - Passing in a property field of a class

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; }
like image 659
user1781232 Avatar asked Oct 17 '25 13:10

user1781232


2 Answers

You can do something like:

[RegularExpression("^(07(\\d ?){9})", ErrorMessage = "Invalid Phone Number")]
like image 173
Moe Bataineh Avatar answered Oct 20 '25 03:10

Moe Bataineh


If you change your declaration of MobileFormat to be a const, this should fix the problem

public const string MobileFormat = "^(07(\\d ?){9})";
like image 20
Patrick McDonald Avatar answered Oct 20 '25 04:10

Patrick McDonald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!