Date format in RangeValidator
Asked Answered
S

2

7

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");
}
Singular answered 26/7, 2010 at 6:19 Comment(0)
H
8

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");
}
Heighho answered 27/7, 2010 at 6:58 Comment(0)
E
4

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

Evidence answered 5/1, 2015 at 15:10 Comment(7)
for the application that I am writing I want the date format to be MM/dd/yyyy so it should be whatever it needs to be for your application. And your link is brokenAdriaadriaens
@Malachi: No. the MinimimValue should always be in format yyyy/MM/dd. The validator uses that format to validate. It's independent of culture.Evidence
Where does it say that? the article that you linked to says nothing about Date ranges.Adriaadriaens
forums.asp.net/t/1343117.aspx?Date+format+for+RangeValidator the accepted answer says that the date format is taken from the machine the code is housed on. and it also gives @MikeM's answer of how to resolve the situation to another formatAdriaadriaens
in the MSDN it states: "If you specify ValidationDataType.Date for the BaseCompareValidator.Type property without programmatically setting the culture for the application, you should use a culture-neutral format, such as YYYY/MM/DD, for the MaximumValue and MinimumValue properties. Otherwise, the date may not be interpreted correctly." So only when no culture is defined. However I had same problem as in question. Application culture was nl-NL, daterange date format was NL, did not work. Using the culture-neutral format YYYY/MM/DD fixed the problem.Evidence
I guess if it is on the back end it doesn't really matter if it doesn't match the user's expected format.Adriaadriaens
the format should match the backend (server side) culture. If that does not work use YYYY/MM/DD.Evidence

© 2022 - 2024 — McMap. All rights reserved.