tags:

views:

51

answers:

3

In my application user can specify the pattern for dates by entering it in the textbox. This pattern is used for logging messages. For example, if user specify 'dd-MM' then in the log file he could see following:

[12-06] Some message...
[02-09] Some message 2...

How to validate this pattern? How to protect entering wrong patterns?

+4  A: 

The easiest way is to just try and parse the date.

DateTime someDate;

// Valid will be false if it could not parse the date
bool valid = DateTime.TryParse(yourFormat, out someDate);
David Basarab
I would add, if you need to validate on the client browser, just use a regular expression.
Nordes
A: 

Regular Expressions will probably be a good choice.

Itay
A: 

You can take a look at Regular Expressions or else, at this question

npinti