Link to home
Start Free TrialLog in
Avatar of ksummers
ksummers

asked on

Range validator problem in C#.NET to check date range.

I am having a problem with a range validator which I had asked a question about a few weeks ago.  Here is what is happening, and I actually created a simple ASP.NET page to test this with a textbox and a rangevalidator.  I set the maximum value of the rangevalidator but it DOES NOT pop up a warning if I put in todays today, which it should.  What am I doing wrong?

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtToDate.Text = DateTime.Now.AddDays(-1).ToShortDateString();  //Default to yesterday's date.
        RangeValidator1.MaximumValue = DateTime.Now.AddDays(-1).ToShortDateString();

    }
}
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

have you included the property inside the validator declaring its type. so for example

        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="RangeValidator"
            Type="Date"></asp:RangeValidator>
check this

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Error!!!!!!"
            Type="Date" ControlToValidate="TextBox1" MaximumValue="1983-04-21" MinimumValue="1983-04-17"></asp:RangeValidator>

if i now put in 1983-04-23 it will alert me the error but a range inside the allowed range will work

Andrew
Avatar of ksummers
ksummers

ASKER

I have to set the maximum value in the code like I have it, but I do not get the error if I type in tomorrow's date 10/2/2007.  I don't want to hardcode it.  Why won't it get me the error?

I tried to change the date type but I get this error.  Did you get this error with my code??

The value '' of the MinimumValue property of 'RangeValidator1' cannot be converted to type 'Date'.

ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is there a way to specify the format then?  I need it to be entered as mm/dd/yyyy and I need the validator to accept this and check it??
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox><br />
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Error!!!!!!" Type="Date" ControlToValidate="txtToDate" MaximumValue="2010-10-01" MinimumValue="1000-01-01"></asp:RangeValidator></div>

Thanks, but I figured it out.  I set the date and set the minimum to a crazy early date and set the maximum to yesterday's date using the datetime function.  It works fine.  I will award you the points.