I am using RangeValidator to validate date enter in textbox and its working fine with default date format but now i want the date format in “dd/MM/yyy” but its generating excption with this date format. please provide me solution
my code:
in aspx page:
<asp:TextBox ID="txtrequiredby" runat="server" ></asp:TextBox > <cc1:CalendarExtender ID="txtrequiredby_CalendarExtender" Format="dd/MM/yyyy" runat="server" Enabled="True" TargetControlID="txtrequiredby" > </cc1:CalendarExtender > <asp:RangeValidator ID="rvreqby" runat="server" ErrorMessage="Required By Date Greater Than or Equal to current date" ControlToValidate="txtrequiredby" Display="Dynamic" Type="Date" ></asp:RangeValidator >
in codebehind:
rvreqby.MinimumValue = clsGeneral.FromSqlDate( DateTime.Now);
rvreqby.MaximumValue = clsGeneral.FromSqlDate( DateTime.Now.AddYears(200));
public static string FromSqlDate(DateTime date)
{
return date.ToString("dd/MM/yyyy");
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
MinimumValue & MaximumValue need to be set in the Page_PreRender event and appear to require the date format as “dd-MM-yy”…see last post on Rangevalidator Min Max Value error
protected void Page_PreRender(object sender, EventArgs e)
{
RangeValidator1.MinimumValue = DateTime.Now.Date.ToString("dd-MM-yy");
RangeValidator1.MaximumValue = DateTime.Now.Date.AddYears(90).ToString("dd-MM-yy");
}
Method 2
Format of the MinimumValue and MaximumValue should be yyyy/MM/dd
Check documentation here:
https://msdn.microsoft.com/en-us/library/ydez7ad7(v=vs.110).aspx
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0