Hi all,
I want to validate the text box in gridview for date format.
how can I do this.
Thanks in Advance
Hi all,
I want to validate the text box in gridview for date format.
how can I do this.
Thanks in Advance
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:
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:
Reference reading:
Jump to Post— Mind Bird 0in 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>
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.