Validate Date with Regular Expression in ASP.NET

No.of Views2391
Bookmarked0 times
Downloads 
Votes0
By  Troy   On  15 Feb 2010 23:02:08
Tag : ASP.NET , Validation Controls
Validate Date with Regular Expression in ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


Here we have to discuss the Regular expression ie going to validate date on different way ie: different date format

Technologies:

ASP.NET

Language:

C# and VB.NET


Prerequisite

.NET Framework 2.0


Visual Studio 2005


Implementation Code


It depends on which is the format you want to use. For a date format like: yyyy-mm-dd you could write it like:

(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])

HTML CODE

hi, so what is the valid input? anyway, you can do something like this

{codecitation class="brush: csharp; gutter: true;" width="650px"}

"TextBox6" runat="server">
"RegularExpressionValidator2" runat="server" ControlToValidate="TextBox6"
ErrorMessage="RegularExpressionValidator" ValidationExpression="(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])">
Invalid Date Format: (yyyy-mm-dd)

{/codecitation}

Here i am going to give different format of date expression you can change only the validationExpression

^((0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](?:19|20)\d\d)$ -- Invalid Date Format: (dd-mm-yyyy)

You can use dd MM yyyy HH:mm:ss with this expression:

(0[1-9]|[12][0-9]|3[01])\s(0[1-9]|1[012])\s(19|20)\d\d\s([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])

To use the moth in the MMM format you would have to provide each moth as an option in the expression. Here is one example for January, February and March

(0[1-9]|[12][0-9]|3[01])\s(jan|fev|mar|etc)\s(19|20)\d\d\s([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])

Thank you

Venkat

 
Sign Up to vote for this article
 
About Author
 
Troy
Occupation-Not Provided
Company-Not Provided
Member Type-Junior
Location-Not Provided
Joined date-10 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top