RangeDateValidator en asp.net ne fonctionne pas comme prévu
-
27-10-2019 - |
Question
J'ai mis le RangeDateValidator dans le code derrière comme indiqué ci-dessous, et même si j'entrer une valeur correcte telle que 11/11/1990, il provoque toujours une erreur de validation.
public void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DOBRangeValidator.MinimumValue = DateTime.Now.AddYears(-100).ToString("dd/MM/yyyy");
DOBRangeValidator.MaximumValue = DateTime.Now.AddYears(-12).ToString("dd/MM/yyyy");
}
}
Voici la page .net
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="DOBLabel" runat="server" AssociatedControlID="DOBTextBox" Text="Date of Birth:"/>
</asp:TableCell><asp:TableCell>
<asp:TextBox ID="DOBTextBox" runat="server" Height="25px" Width="200px" Text="dd/mm/yyyy"/>
</asp:TableCell><asp:TableCell>
<asp:RequiredFieldValidator ID="DOBRequired" runat="server"
ControlToValidate="DOBTextBox" InitialValue="dd/mm/yyyy" ErrorMessage="Date of birth is required."
ToolTip="Date of birth is required." Display="Dynamic" ValidationGroup="RegistrationForm"/>
<asp:RangeValidator ID="DOBRangeValidator" ControlToValidate="DOBTextBox" runat="server" ErrorMessage="Please enter your correct DOB (Must be over 12)" ValidationGroup="RegistrationForm"/>
</asp:TableCell></asp:TableRow><asp:TableRow>
La solution
You need to set the type of the RangeValidator to Date otherwise it will do a string comparison
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow