Question

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.

Was it helpful?

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>

OTHER TIPS

Set the type Integer when you are using RangeValidator for integer type values.

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

But I am surprised that you are using RangeValidator with Password field. In this case you are restricting user to put value between 4-13. You probably wants to check the length of input. For that you use Regular Expression validator.

<asp:RegularExpressionValidator ID="RegexVal" ValidationExpression="^.{4,13}$"  runat="server" ErrorMessage="Password must be 4-13 character long" ControlToValidate="TextBox2"  />

The specified data type that you are going to check, currently, is set to string. "4" is greater than "13", that's why you get such an error. Change the Type argument in the control to Integer and it should work.

The Type should be Integer rather than String

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

Google translated the error message below. Password must consist of 4-13 characters.

PashaPash' answer https://stackoverflow.com/a/21060857/263003 is the correct one

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top