Regular coincidence of the first 28 days of the month

I am looking for a regular expression to match only if the date is in the first 28 days of the month. This is for my ASP.NET validator control

+3
source share
5 answers

Do not do this with Regex. Dates are formatted differently in different countries. Use the DateTime.TryParse procedure instead:

DateTime parsedDate;

if ( DateTime.TryParse( dateString, out parsedDate) && parsedDate.Day <= 28 )
{
 // logic goes here.
}

Regex is almost a golden hammer of input validation, but in this case it is the wrong choice.

+18
source

I do not think this is a task very suitable for regular expression.

(DateTime.Parse .NET), , . .

+2

? , , .

+1
  ([1-9]|1\d|2[0-8]) // matches 1 to 28 but woudn't allow leading zeros for single digits
(0?[1-9]|1\d|2[0-8]) // matches 1 to 28 and would allow 01, 02,... 09

(\d , [0-9], .)

. datetime (2008-09-01 12:35:45)?

+1

DateTime.TryParse CustomValidator

+1

Source: https://habr.com/ru/post/1696732/


All Articles