We help IT Professionals succeed at work.

Rangevalidator date problem.

1,602 Views
Last Modified: 2013-11-27
I asked this question before about checking the date with a range validator.  I need the maximum value to be set to yesterday's day in my form!  It seems to be setting the date correctly, but I do NOT get the range validator text warning msg when I enter today's date or a future date.  Please help me.

This seems to be happening at when the month changes.  If I set the date in my code -2 days which would be 9/30/2007, but then type in 10/01/2007 in the textbox, the warning msg does not appear.  I tried setting the type in the range validator to date, but I get a string error.  Please help.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtToDate" runat="server">10/01/2007</asp:TextBox>
        <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtToDate"
            ErrorMessage="RangeValidator">Test!</asp:RangeValidator></div>
    </form>
</body>
</html>

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(-2).ToShortDateString();  //Default to today's date.
        RangeValidator1.MaximumValue = DateTime.Now.AddDays(-2).ToShortDateString();
    }
}

Comment
Watch Question

CERTIFIED EXPERT

Commented:
you need to set RangeValidator's minimum value too:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RangeValidator1.MinimumValue=DateTime.Now.ToShortDateString();
        RangeValidator1.MaximumValue = DateTime.Now.AddDays(-2).ToShortDateString();    
   }
}


CERTIFIED EXPERT

Commented:
also set validator type to "Date"

      <div>
        <asp:TextBox ID="txtToDate" runat="server">10/01/2007</asp:TextBox>
        <asp:RangeValidator ID="RangeValidator1" runat="server" Type="Date" ControlToValidate="txtToDate"
            ErrorMessage="RangeValidator">Test!</asp:RangeValidator></div>
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
<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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.