Date validator when formatted

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>
 
S

Stan

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>

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top