Pergunta

I have set the RangeDateValidator in the code behind as shown below, and even though I enter a correct value such as 11/11/1990, it still causes a validation error.

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");  
    }
}

Below is the .net page

<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>
Foi útil?

Solução

You need to set the type of the RangeValidator to Date otherwise it will do a string comparison

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top