【问题标题】:asp:CompareValidator with other date formats other than d/M/yyyy?asp:CompareValidator 与 d/M/yyyy 以外的其他日期格式?
【发布时间】:2015-06-17 01:09:43
【问题描述】:

我将 VS2005 与 ASP.net 2 和 SQL Server 2005 一起使用

我想使用 asp:CompareValidator 验证文本框日期字段,但我希望它验证 d/M/yyyy 格式(验证器的默认值)和自定义日期格式,如 dd MM yyyy 甚至 dd MMMM年年。

是否可能或我必须使用 CustomValidator?

这是我的代码

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

【问题讨论】:

    标签: asp.net datetime customvalidator


    【解决方案1】:

    您可以使用asp:RegularExpressionValidator 中定义的http://forums.asp.net/t/1480183.aspx?Using+CustomValidator+to+validate+Date+Format+in+dd+MM+yyyy

    您唯一能找到的是一个可以验证多种日期格式的正则表达式。

    【讨论】:

    • 谢谢。我已经知道我可以做到这一点,但我希望有办法让 CompareValidator 接受其他日期格式,以便使用 prevalidation type="date"。
    • 答案是否定的,您不能“捏造”asp.net 控件,这不是我想要的上述方式。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多