1

What is regular expression format for date dd-mm-yyyy.Following is the code i used but its not working.

 [Required(ErrorMessage = "Activation date is required")]
        [Display(Name = "Activation date")]
        [RegularExpression(@"^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[- / .]([1-9]|0[1-9]|1[0-2])[- / .](1[9][0-9][0-9]|2[0][0-9][0-9])$", ErrorMessage = "Enter proper date")]
user2156088
  • 2,350
  • 5
  • 20
  • 24
  • 1
    Isn't there pre-built validation method? You can validate the format, but not the date logic with regex. – nhahtdh Sep 05 '13 at 08:12
  • 1
    Please check http://stackoverflow.com/questions/3594496/asp-net-mvc-2-model-validation-regex-validator-fails – Amit Sep 05 '13 at 08:15

2 Answers2

0

It would be very difficult to validate a date via regex. For instance, given 29-02-yyyy, how would you check if the given year is a leap year?

A better alternative would be to programatically use a validator. Check out how to use one here.

Another approach would be to use the DateTime.TryParse method. You can easily check the returned boolean value to see if you have a valid date or not.

Community
  • 1
  • 1
Westy92
  • 19,087
  • 4
  • 72
  • 54
0

try this regex

^(((((0[1-9])|(1\d)|(2[0-8]))-((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))-((20[0-9][0-9]))|(29-02-20(([02468][048])|([13579][26]))))$