Riya
Riya

Reputation: 559

Date validation using regular expression

can anyone please tell me how to check whether the entered date(24 hour format) in a textbox is valid or not using regular expression?

Upvotes: 0

Views: 1663

Answers (2)

David Hall
David Hall

Reputation: 33143

There are lots of variations of regex that will work for validating a date.

The site Regular-Expressions.info has some examples of regexes to start you off.

How you actually invoke the regular expression depends upon the environment you are using. In c# there is the regex class, in javascript you can use the RegExp() object.

What environment are you in?

Also, in most cases I think you would be better off using something other than a regex. For example, the DateTime struct in c# allows for date validation with its .Parse() and .TryParse() methods while Asp.Net has various validator classes, as does MVC.

Upvotes: 0

jb.
jb.

Reputation: 10341

You should not use a regex to solve this. Use the DateTime.TryParse method.

DateTime dt;

bool bSuccess = DateTime.TryParse("2009-05-01 14:57:32", out dt);
if(bSuccess)
    Console.WriteLine("it's a date!");

Upvotes: 2

Related Questions