Reputation: 97
I have a simple ASP.NET page, and in it I am trying to validate a date. The date picked must be greater than or equal to today's date.
I am getting an error which says "The value '26/04/2104 13:55:38' of the MaximumValue property of 'DateCheck' cannot be converted to type 'Date'" when I try to do this.
Here is the code in the aspx:
Date: <asp:Calendar ID="txtDate" runat="server" BackColor="White"
BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest"
Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px"
Width="200px">
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<NextPrevStyle VerticalAlign="Bottom" />
<OtherMonthDayStyle ForeColor="#808080" />
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<WeekendDayStyle BackColor="#FFFFCC" />
</asp:Calendar><br /><br />
<asp:RangeValidator ID="DateCheck" runat="server" ErrorMessage="The date must be greater than or equal to current date" ControlToValidate="txtDate"
Display="Dynamic" Type="Date" ></asp:RangeValidator >
and here is the code behind file:
protected void Page_PreRender(object sender, EventArgs e)
{
DateCheck.MinimumValue = DateTime.Now.ToString();
DateCheck.MaximumValue = DateTime.Now.AddYears(90).ToString();
}
Can anyone help with solving this?
Upvotes: 0
Views: 5201
Reputation: 1842
Format of the MinimumValue and MaximumValue should be yyyy/MM/dd
.
It is solved my problem.
Upvotes: 1
Reputation: 133
I guess Easily you can achieve this through Javascript My way is below
Firstly get todays Day like this
function validate_date(date_to_validate)
{
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var vdate[]=date_to_validate.split('/');// Assuming Formated Date You will pass in mm/dd/yyyy
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
if(vdate[2]>yyyy+90 ||vdate[2]<yyyy)
{
return false;
}else if(vdate[2]==yyyy && ( vdate[0]<mm ))
{
return false;
}else if(vdate[2]==yyyy && (vdate[1]<dd))
{
return false;
}
else
{
return true;
}
}
And this function return true for your min and max date condition else returns false...
I think Range validator Can Support string range ,integer range.... And you validating on server which will cost you extra request and response.... using client side validation you can do better
Upvotes: 1