Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need regular expression for validating date in dd-MMM-yyyy format

Tags:

regex

asp.net

I am not expert in writing regular expressions so need your help. I want to validate date in "dd-MMM-yyyy" format i.e. 07-Jun-2012. I am using RegularExpressionValidator in asp.net.

Can anybody help me out providing the expression?

Thanks for sharing your time.

like image 592
IrfanRaza Avatar asked Jun 07 '12 11:06

IrfanRaza


People also ask

What is regular expression for date format dd mm yyyy?

For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.] (0[1-9]|1[012])[- /.] (19|20)\d\d$. You can find additional variations of these regexes in RegexBuddy's library.

What is the regular expression for date format?

The regex matches on a date with the MM/DD/YYYY format and a "Date of birth:" or "Birthday:" prefix (Year min: 1900, Year max: 2020). For example: Date of birth: 12/01/1900.

How do I validate a pattern in regex?

To validate a field with a Regex pattern, click the Must match pattern check box. Next, add the expression you want to validate against. Then add the message your users will see if the validation fails. You can save time for your users by including formatting instructions or examples in the question description.

Is regex used for validation?

The Validation (Regex) property helps you define a set of validation options for a given field. In general, this field property is used to perform validation checks (format, length, etc.) on the value that the user enters in a field. If the user enters a value that does not pass these checks, it will throw an error.


2 Answers

Using a DatePicker is probably the best approach. However, since that's not what you asked, here's an option (although it's case sensitive):

^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$

In addition, here's a place you can easily test Regular Expressions: http://www.regular-expressions.info/javascriptexample.html

like image 73
Mark Avatar answered Sep 28 '22 10:09

Mark


Regex without leading zero in day.

^\d{1,2}-[a-zA-Z]{3}-\d{4}$

Update Regex with leading zero in day.

^\d{2}-[a-zA-Z]{3}-\d{4}$
like image 25
Leonov Mikhail Avatar answered Sep 28 '22 12:09

Leonov Mikhail