EF Data Annotations Regular Expression

I need to check the correct expression for the first 5 digits are numbers, then a hyphen, and then one digit is an alphabet and the other is a numeric one.

ex: 23456-p5 or 12345-a3 something like that.

I tried something like this to start with

/^\d{5}-\d{2}$/ 

      

and in the model i are given so that we don't need to give ^ or $ in data annotations

[RegularExpression(@"d{5}-\d{2}")] 

      

But I couldn't get it to work even that.

+3


source to share


2 answers


Try the following:



[RegularExpression(@"^\d{5}-[a-zA-Z]\d$")]

      

+3


source


An expression like this should work:



^\d{5}\-[A-Za-z]\d$

      

+1


source







All Articles