?
Solved

Rangevalidator date problem.

Posted on 2007-10-02
4
Medium Priority
?
1,503 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();
    }
}

0
Comment
Question by:ksummers
  • 3
4 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 20000549
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();    
   }
}


0
 
LVL 35

Expert Comment

by:YZlat
ID: 20000573
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>
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1000 total points
ID: 20000599
then do this:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       RangeValidator1.MinimumValue="01/01/1900";
       RangeValidator1.MaximumValue = DateTime.Now.AddDays(-1).ToShortDateString();
   }
}

0
 

Author Comment

by:ksummers
ID: 20000853
<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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question