0

I need to match the Date string to be 1 or 2 numbers.

 [StringLength(2)]
 [RegularExpression(@"\d{1,2}", ErrorMessage = "Date must be 1- 2 numbers")]
 public string Date { get; set; }

I will need to do similar with Year to match 4 digit character string. What am I doing wrong here please ?

mishap
  • 8,176
  • 14
  • 61
  • 92

1 Answers1

0

There is a similar question with accepted answer on so question, it checks for a valid year. It has more validations in answers if you need them.

Regex isn't language dependant so if you need you can just check the web for correct one in any language. If you want to learn how to write them you can check out website, there is an application where you can test your regex.

For date, check out date regex, or if you just want the answer:

(0[1-9]|[12][0-9]|3[01])$

It checks date from 01 to 31

For year

(19|20)\d\d

Matches 1900-2099 :D

Community
  • 1
  • 1
Goran Žuri
  • 1,623
  • 14
  • 24