Regular expression for asp: RegularExpressionValidator with MMddyy format (leap year release)

We need help for a regular expression that works with asp.net asp: RegularExpressionValidator to check the date in MMddyy format. The problem we are facing is a leap year. The problem is that it can be verified through regular expression that it only accepts valid leap year dates, such as 02/29/2008, is a valid date, but 02/29/2010 is not a valid date.

Any regex that works with "asp: RegularExpressionValidator"?

+3
source share
4 answers

OK, you requested a regular expression. Here. I immediately think that it is not recommended to check the date with a regular expression:

Firstly, a detailed, commented version, at least, makes it clear that this beast is possible:

^ # start of string (?: # either match... (?: (?: # 31st day of all allowed months (?:(?:0?[13578]|1[02])/31) | # or (?:(?:0?[13-9]|1[0-2])/(?:29|30)) ) # 29th/30th day of any month except February / # plus any year since 1600 (?:1[6-9]|[2-9]\d) \d{2} ) | # or (?: # match Feb 29th 0?2/29/ (?: # in all leap years since 1600 (?: (?: # century 1[6-9]|[2-9]\d ) (?: # two-digit years divisible by four, not ending in 00 0[48] | [2468][048] | [13579][26] ) | (?: # all the leap years ending in 00 (?:16|[2468][048]|[3579][26]) 00 ) ) ) ) | # or (?: # (for any month) (?:0?[1-9]) | (?:1[0-2]) ) / (?: # match the 1st-28th day 0?[1-9]|1\d|2[0-8] ) / (?: (?:1[6-9]|[2-9]\d)\d{2} ) )$ 

Or, if you cannot use verbose regular expressions in ASP.NET validators:

 ^(?:^(?:(?:(?:(?:(?:0?[13578]|1[02])/31)|(?:(?:0?[13-9]|1[0-2])/(?:29|30)))/(?:1[6-9]|[2-9]\d)\d{2})|(?:0?2/29/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))/(?:0?[1-9]|1\d|2[0-8])/(?:(?:1[6-9]|[2-9]\d)\d{2}))$)$ 

They allow, but do not require an initial zero in one-bit months / days. If you do not want this, replace all instances of 0? by 0 .

+15
source

If the server side is not an option, you will have to use JavaScript. Try the published code and explain here . It determines that 02/29/2010 is invalid and valid on 02/29/2008. Use CustomValidator to call a JavaScript function if necessary.

+2
source

Since you need logic to check leap years, consider using CustomValidator . I set it up relatively quickly, but I hope you get this idea.

 protected void dateFormatValidator_ServerValidate(object source, ServerValidateEventArgs args) { if (args.Value.Length == 6) { var month = args.Value.Substring(0, 2); var day = args.Value.Substring(2, 2); var year = args.Value.Substring(4, 2); DateTime dummyValue; args.IsValid = DateTime.TryParse(month + "/" + day + "/" + year, out dummyValue); } else { args.IsValid = false; } } 
+1
source

Bit by bit, clientide JS is a way to make this not a terrible regular expression a la Tom mindboggling post. I have a pretty neat date checking mechanism on my work machine, which I will post on Monday.

If you ever get some kind of unsuccessful error with your application, can you imagine a nightmare trying to decrypt this regular expression?

0
source

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


All Articles