문제

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>
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top