11

hey..i would like to compare the current date with the date entered by user..however, i'm encountering errors so far..

i tried something like this:

<asp:TextBox id="txtDate1" runat="server" />    
<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
    ControlToValidate="txtDate1" type="date" 
    ValuetoCompare="DateTime.Today.ToShortDateString()" />

and i got an error stating that the value of DateTime.Today.ToShortDateString() of the ValueToCompare property of "" cannot be converted to type 'date' i also tried ValueToCompare="DateTime.Now.Date()" and i got the same error message.

please help me and i greatly appreciate it.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
HelloBD
  • 387
  • 4
  • 14
  • 27
  • We can set the ValueToCompare in code behind. `Comparevalidator1.ValueToCompare = DateTime.Today.ToShortDateString();` – LCJ Jun 12 '13 at 09:29

5 Answers5

17

You're just using the ValueToCompare property as a literal string. You need to use ASP tags in it if you want to execute code to get a dynamic value. Try this:

<asp:comparevalidator runat="server" 
  errormessage="The date must be greater than today"
  controltovalidate="txtDate1" type="date" 
  valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />

Then in your Page_Load method, call Page.DataBind().

This will execute the databinder code when the page is loaded, and put the value in between the quotes.

womp
  • 115,835
  • 26
  • 236
  • 269
  • this show me The value '' of the ValueToCompare property of 'Comparevalidator1' cannot be converted to type 'Date'. – HelloBD Feb 15 '10 at 06:21
  • Sorry - I forgot to mention that you'll also need to call "Page.DataBind()" in your Page_Load method. – womp Feb 15 '10 at 21:34
  • 2
    Just wondering, if this requires you to call `DataBind()` in the Page_Load, couldn't you just as well write `MyCompareValidator.ValueToCompare = DateTime.Today.ToShortDateString();` in the Page_Load? – comecme Nov 05 '11 at 10:08
  • 4
    You miss the `Operator="GreaterThan"` otherwise it will treat as **Equal** – CallMeLaNN Sep 21 '12 at 03:08
  • @comecme I have posted an answer by setting `ValueToCompare` in code behind. – LCJ Jun 13 '13 at 11:57
  • Why do you have to use #? Is this a databinding operation? – ErTR Oct 07 '16 at 11:40
7
    <asp:CompareValidator ID="CompareValidator3" runat="server" 
                        ControlToValidate="TextBox1" ErrorMessage="Date should be on or after today" 
                        Operator="GreaterThanEqual" Type="Date">
</asp:CompareValidator>

In the page load event set the validator's value to compare as

CompareValidator3.ValueToCompare = DateTime.Now.ToShortDateString();
Abhishek kumar
  • 2,586
  • 5
  • 32
  • 38
1

We can set the ValueToCompare in code behind

        if (!Page.IsPostBack)
        {
           string currentDate = DateTime.Today.ToShortDateString();
           Comparevalidator1.ValueToCompare = currentDate;
        }

for the compare validator:

    <asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
    Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />

Why not use Page.DataBind?

Consider the following scenario. I need to display the gridview only on the click of the Action button. The datasource is defined declaratively. But, if I use Page.DataBind() it will show the grid even on the page load.

 <form id="form1" runat="server">
 <asp:TextBox ID="txtDate1" CssClass="firstBox" runat="server" Text=""></asp:TextBox>
 <asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
    Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />
 <asp:Button ID="btnAction" class="submitButton" runat="server" Text="Action" OnClick="btnAction_Click" />
 <asp:Button ID="btnDummy" class="submitButton" runat="server" Text="Dummy" OnClick="btnDummy_Click" />
 <br />
 <br />

 <asp:GridView ID="GridView1" runat="server" DataSource="<%# EmployeesResult %>">
 </asp:GridView>
 </form>

Code behind

public partial class ThirdTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.DataBind();

        if (!Page.IsPostBack)
        {
           string currentDate = DateTime.Today.ToShortDateString();
           txtDate1.Text = currentDate;
           Comparevalidator1.ValueToCompare = currentDate;
        }
    }

    protected void btnAction_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();
        string value = GridView1.DataSource.ToString();
    }

    protected void btnDummy_Click(object sender, EventArgs e)
    {

    }

    //Propertry
    public List<Employee> EmployeesResult
    {
        get
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { EmpID = 1, EmpName = "Emp1" });
            employees.Add(new Employee { EmpID = 2, EmpName = "Emp2" });
            return employees;
        }
    }
  }
LCJ
  • 22,196
  • 67
  • 260
  • 418
0

Try this.

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Date is required" ControlToValidate="txtmDate"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Date is not valid (MM.DD.YYYY)" ControlToValidate="txtDate" ValidationExpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" ></asp:RegularExpressionValidator>

The way user input the date (date format) is also Important. In here, I have used MM.DD.YYYY date format.

<asp:CompareValidator ID="CompareValidatorGreaterThanToday" runat="server" ErrorMessage="The date must be greater than today" ControlToValidate="txtDate" Type="date" Operator="GreaterThan" ValueToCompare="<%# DateTime.Today.ToShortDateString() %>" ></asp:CompareValidator>

Then in your Page_Load method (*.aspx.cs), call Page.DataBind().

Example:

protected void Page_Load(object sender, EventArgs e)
{            
   Page.DataBind();
}
DxTx
  • 3,049
  • 3
  • 23
  • 34
-1

Try the below written :

<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
ControlToValidate="txtDate1" type="DateTime" 
ValuetoCompare='<%# DateTime.Now.ToString("d") '%> />
Learner
  • 1,544
  • 8
  • 29
  • 55