Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4790
  • Last Modified:

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();

    }
}
0
ksummers
Asked:
ksummers
  • 3
  • 3
1 Solution
 
REA_ANDREWCommented:
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>
0
 
REA_ANDREWCommented:
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
0
 
ksummersAuthor Commented:
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'.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
REA_ANDREWCommented:
yeh it is the format of the date you are entering where the problem lies. for me yyyy-MM-dd works fine.

Andrew
0
 
ksummersAuthor Commented:
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??
0
 
ksummersAuthor 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.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now