Date validator when formatted

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

I am trying to use a CompareValidator to validate an entered date in my
asp.net page. The date is being formatted on display and apparently the
CompareValidator does not like that. Is there a way around this? below is
the section of GridView where the problem exists. Thanks.
David

<asp:TemplateField HeaderText="Activity Date"
SortExpression="ActivityDate" ItemStyle-Width="120">
<EditItemTemplate>
<asp:TextBox ID="txtActivityDate" runat="server"
Text='<%# Bind("ActivityDate", "{0:M/d/yyyy h:m tt}") %>'></asp:TextBox>
<asp:CompareValidator ID="valActivityDate"
runat="server" ErrorMessage="Activity Date must be a valid date"
Operator="DataTypeCheck" Type="Date"
Display="Dynamic"
ControlToValidate="txtActivityDate"></asp:CompareValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("ActivityDate", "{0:M/d/yyyy h:m tt}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
 
I am trying to use a CompareValidator to validate an entered date in my
asp.net page. The date is being formatted on display and apparently the
CompareValidator does not like that.  Is there a way around this?  below is
the section of GridView where the problem exists. Thanks.
David

                <asp:TemplateField HeaderText="ActivityDate"
SortExpression="ActivityDate" ItemStyle-Width="120">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtActivityDate" runat="server"
Text='<%# Bind("ActivityDate", "{0:M/d/yyyy h:m tt}") %>'></asp:TextBox>
                        <asp:CompareValidator ID="valActivityDate"
runat="server" ErrorMessage="Activity Date must be a valid date"
                                Operator="DataTypeCheck" Type="Date"
Display="Dynamic"
ControlToValidate="txtActivityDate"></asp:CompareValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#
Bind("ActivityDate", "{0:M/d/yyyy h:m tt}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

Hi

You may be using the wrong type of validator. The CompareValidator
works by comparing the input with another control value or a constant.
You dont specify either in your code.

Perhaps a RegularExpressionValidator would be more appropriate.
 
Back
Top