Question CompareValidator How to compare date according to Today?

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,

I would like to restrict entry of a date if date entered is less than today. I tried to put it inside Compare Validator but could not managed.

Thanks in advance.


Best Regards

Here is the code below
VB.NET:
<asp:TextBox runat="server" ID="TextBox_tarih" Text=""></asp:TextBox>
                    <asp:CalendarExtender ID="TextBox_tarih_CalendarExtender" runat="server" Format="dd/MM/yyyy"
                        TargetControlID="TextBox_tarih">
                    </asp:CalendarExtender>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox_tarih"
                        ErrorMessage="Tarihi Seçiniz" SetFocusOnError="True" Font-Bold="True"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox_tarih"
                        ControlToCompare="<% Date.Now() %>" Operator="LessThan" Type="Date"
                        ErrorMessage="Bugünün Tarihinden küçük olamaz..."></asp:CompareValidator>
 
You can use RangeValidator instead. e.g.
PHP:
    <asp:TextBox runat="server" ID="TextBox_tarih" Text="" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please select a date" ControlToValidate="TextBox_tarih" />
    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox_tarih" ErrorMessage="*Please enter a date less than today!" Type="Date" Display="Dynamic" />

then in your page load event we set its maximum and minimum date values

VB.NET:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RangeValidator1.MaximumValue = DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy")
        RangeValidator1.MinimumValue = New DateTime(1715, 1, 1).ToString("dd/MM/yyyy")
    End Sub
 

Latest posts

Back
Top