asp:CompareValidator with other date formats other than d/M/yyyy?

740 views Asked by At

I am using VS2005 with ASP.net 2 and SQL Server 2005

I want to use the asp:CompareValidator to validate a text box date field, but I want it to validate both d/M/yyyy format (default of the validator) and custom date formats like dd MM yyyy or even dd MMMM yyyy.

Is it possible or do I have to use a CustomValidator?

Here is my code

                                <asp:TemplateField HeaderText="Date of Birth" SortExpression="StudentDOB">
                                    <InsertItemTemplate>
                                        <asp:TextBox ID="tbDOB" runat="server" Text='<%# Bind("StudentDOB","{0:d/M/yyyy}") %>'></asp:TextBox>
                                        <asp:ImageButton runat="Server" ID="Image1" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" CausesValidation="False" />
                                        <ajaxToolkit:CalendarExtender PopupButtonID="Image1" Format="d/M/yyyy" TargetControlID="tbDOB" ID="CalendarExtender1" runat="server"></ajaxToolkit:CalendarExtender>
                                        <asp:RequiredFieldValidator ID="rfvDOB" runat="server" ControlToValidate="tbDOB" ErrorMessage="* required">* Required</asp:RequiredFieldValidator>
                                        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="tbDOB" Display="Dynamic" ErrorMessage="Invalid Date" Operator="DataTypeCheck" Type="Date" />
                                    </InsertItemTemplate>
                                    <ItemStyle HorizontalAlign="Left" />
                                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
                                </asp:TemplateField>
1

There are 1 answers

2
ManP On BEST ANSWER

You can use a asp:RegularExpressionValidator as defined in http://forums.asp.net/t/1480183.aspx?Using+CustomValidator+to+validate+Date+Format+in+dd+MM+yyyy

The only thing you have find is a regex that can validate multiple date formats.