Hi all,


I want to validate the text box in gridview for date format.


how can I do this.


Thanks in Advance

Dani AI

Generated

Nice call by — client-side regular expressions are great for quick feedback and they solve many simple cases. A couple of practical additions will make the validation robust and user-friendly for real apps.

Use server-side parsing to guarantee a valid calendar date (regexes can still let through things like 30/02 or mis-hits on leap years). In the GridView RowUpdating (or RowCommand) handler, locate the edited TextBox in the row, then parse the text with DateTime.TryParseExact or DateTime.TryParse using an explicit culture or format list. If parsing fails, cancel the update and show an error (or set a CustomValidator error). Example workflow:

  • find the TextBox in the editing row,
  • attempt TryParseExact (one or more formats) or TryParse with the expected culture,
  • if false, stop the update and report a validation message.

Improve UX to avoid format confusion. Attach a date picker to the edit TextBox (server-side calendars or a client widget such as the jQuery UI Datepicker) so users enter a normalized value and validation becomes simpler. That also reduces the need for multiple allowed formats.

Extra tips and gotchas:

  • Put validators inside the EditItemTemplate so they validate the right control instance, and set ValidationGroup on the row update button if you use grouped submission.
  • Always enforce the check on the server; client-side validators can be bypassed.
  • If you must accept multiple user formats, parse with TryParse using a CultureInfo or test several TryParseExact formats in a controlled order.

Reference reading:

Recommended Answers

All 2 Replies

in grid view use textbox control like this:-
<templatefiled>
<itemtemplate>label<itemtemplate>
<edittemplate>textbox<edittemplate>
</templatefield>
in textbox
use this code
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="Date must be in the form DD/MM/YYYY"
ControlToValidate="tbDiaryDateDue"
ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"
Display="Dynamic"></asp:RegularExpressionValidator>

<asp:RegularExpressionValidator id="RegularExpressionValidator2" runat="server" ErrorMessage="Date must be in the form D/M/YYYY"
ControlToValidate="tbDiaryDateDue"
ValidationExpression="([1-9]|[12][0-9]|3[01])[- /.]([1-9]|1[012])[- /.](19|20)\d\d"
Display="Dynamic"></asp:RegularExpressionValidator>

Thanks to Mind Bird,

For you reply.

It works.

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.