Search code examples
c#asp.netrangevalidator

An error when specify range for RangeValidator


I try to specify range from 4 to 13. But it keeps error "The MaximumValue 13 cannot be less than the MinimumValue 4 of RangeValidator1." How can I solve this. Here's my code:


     <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="Group1"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                ControlToValidate="TextBox2" ErrorMessage="กรุณากรอก Password" ForeColor="Red" 
                ValidationGroup="Group1">*</asp:RequiredFieldValidator>
     <asp:RangeValidator ID="RangeValidator1" runat="server"
                ControlToValidate="TextBox2" 
                ErrorMessage="Password ต้องมีความยาวตั้งแต่ 4-13 ตัวอักษร" ForeColor="Red" 
                MaximumValue="13" MinimumValue="4" Type="String" EnableClientScript="false">*</asp:RangeValidator>

This is the code in Button:


    protected void Button2_Click1(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {

            }
            else
            {
                Insert();
            }
        }
        catch (Exception ex)
        {

        }
    }

Any help appreciated.


Solution

  • RangeValidator validates the value of the control, not the value length. For string comparison "13" is less than "4", so you are getting the "max < min" error.

    You should use RegularExpressionValidator to check the input length:

     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                ControlToValidate="TextBox2" 
                ErrorMessage="Password ต้องมีความยาวตั้งแต่ 4-13 ตัวอักษร" ForeColor="Red" 
                ValidationExpression="^.{4,13}$" ValidationGroup="Group1" EnableClientScript="false">*</asp:RegularExpressionValidator>