Saturday, 7 December 2013

How to enter Date in a given format in a TextBox in ASP.NET?

In ASP.NET, date can be validated easily by using RegularExpressionValidator. Suppose that you want your user to enter the date in (DD-MM-YYYY) format in a TextBox named Date_of_Birth. To do so, you just need to set the following RegularExpressionValidator properties:

ID Date_Validator
ControlToValidate Date_of_Birth
ErrorMessage Please fill the date in correct format
ValidationExpression ^(\d{2})\-(\d{2})\-(\d{4})$

If you want your user to enter the date in (DD/MM/YYYY) format, then ValidationExpression property will be as follows:

ValidationExpression ^(\d{2})\/(\d{2})\/(\d{4})$

If you want your user to enter the date in (D-M-YY) format, then ValidationExpression property will be as follows:

ValidationExpression ^(\d{1,2})\-(\d{1,2})\-(\d{2})$

(Note: You can change Error Messages, Control Names and Formats according to your project requirements.)

No comments:

Post a Comment