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.
<asp:TextBox
ID="TextBox1"
runat="server"></asp:TextBox>
<asp:CompareValidator
ID="CompareValidator1"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage="CompareValidator"
Operator="GreaterThanEqual"
Type="Date"></asp:CompareValidator>
And Try to put this on page_load eventprotected
void Page_Load(object sender,
EventArgs e)
{
CompareValidator1.ValueToCompare =
DateTime.Now.ToShortDateString();
}
Hope this is help ..
DC517 Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
Marked as answer by christina_rules on May 31, 2007 03:07 PM
I'm not sure if this is the best way for your particular case, but it works in mine-- I usually have to compare dynamic dates. I.e., to calculate whether a person's age is correct, it must be between his/her birth date and Now.
For this, I set the compare value dynamically at run time. That way, you don't have to worry about the "cannot be converted to type 'date'" error.
Another possibility could be to use asp code to dynamically assign the date value; You just need to make sure you call Page.DataBind() in your code behind.
<asp:comparevalidator runat="server" errormessage="The date must be greater than today"
controltovalidate="txtDate1" type="date"
valuetocompare='<%# DateTime.Now.Date() %>' />
---------------------------------------
MCP - Web Based Client Development .NET 2.0
<asp:comparevalidator runat="server" errormessage="The date must be greater than today"
controltovalidate="txtDate1" type="date"
valuetocompare='<%# DateTime.Now.ToShortDateString() %>' />
Hey the one u provide is the same as above. how do u stupidly think that this will works? it gave the same result.. better check ot out yourself and post here..
<asp:comparevalidator runat="server" errormessage="The
date must be greater than today" controltovalidate="txtDate1" type="date" valuetocompare='<%= DateTime.Now.ToShortDateString()
%>' />
christina_ru...
Member
55 Points
100 Posts
date compare validator problem
May 31, 2007 02:25 AM|LINK
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.
d4dennis@ins...
Star
9229 Points
1314 Posts
Re: date compare validator problem
May 31, 2007 02:41 AM|LINK
Hi There,
Your compare validator something like this:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="CompareValidator" Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator> And Try to put this on page_load eventprotected void Page_Load(object sender, EventArgs e){
CompareValidator1.ValueToCompare =
DateTime.Now.ToShortDateString();}
Hope this is help ..Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
ps2goat
Star
10845 Points
1977 Posts
Re: date compare validator problem
May 31, 2007 02:48 AM|LINK
I'm not sure if this is the best way for your particular case, but it works in mine-- I usually have to compare dynamic dates. I.e., to calculate whether a person's age is correct, it must be between his/her birth date and Now.
For this, I set the compare value dynamically at run time. That way, you don't have to worry about the "cannot be converted to type 'date'" error.
Another possibility could be to use asp code to dynamically assign the date value; You just need to make sure you call Page.DataBind() in your code behind.
MCP - Web Based Client Development .NET 2.0
narik
Member
259 Points
106 Posts
Re: date compare validator problem
May 31, 2007 03:04 AM|LINK
christina_ru...
Member
55 Points
100 Posts
Re: date compare validator problem
May 31, 2007 03:09 PM|LINK
d4dennis@inspir3, thanks a lot..it helps
narik, i tried yours but i couldn't get it working..
Matt-dot-net
Contributor
5262 Points
989 Posts
Re: date compare validator problem
Jun 11, 2007 09:08 PM|LINK
Try this instead:
datetime Compare Validator
tinaunglatt
Member
2 Points
3 Posts
Re: date compare validator problem
Jun 13, 2007 03:49 AM|LINK
Hey the one u provide is the same as above. how do u stupidly think that this will works? it gave the same result.. better check ot out yourself and post here..
Matt-dot-net
Contributor
5262 Points
989 Posts
Re: date compare validator problem
Jun 13, 2007 03:51 AM|LINK
Sorry... should have "=" instead of "#"
<asp:comparevalidator runat="server" errormessage="The date must be greater than today"
controltovalidate="txtDate1" type="date"
valuetocompare='<%= DateTime.Now.ToShortDateString() %>' />
also need to add operator="GreaterThan"
datetime Compare Validator
kashmirindot...
Participant
978 Points
186 Posts
Re: date compare validator problem
Aug 18, 2008 10:00 AM|LINK
How to compare for DateTime.Now.Tostring("dd/MMM/yyyy")
Tahir.
Matt-dot-net
Contributor
5262 Points
989 Posts
Re: date compare validator problem
Aug 28, 2008 03:11 AM|LINK
Sorry kashmir, the compare validator doesn't recognize your format as a date type. You need to write a custom validator.