asp.net.netvalidationrangevalidator

RangeValidator is allowing several decimal points


If I enter a value of 1.2.1 into my RangeValidator below, it validates OK

<asp:RangeValidator ID="RangeValidator8" runat="server"
    ControlToValidate="TextBoxSAPPlannedHrs" 
    Display="None" ErrorMessage="Must be a valid number 0-999"
    MaximumValue="999" MinimumValue="0"></asp:RangeValidator>

How can this be classed as a valid range. 1.2.1 is not a valid number...


Solution

  • By default if the Type is not specified I think it uses the String as the default conversion type before doing the compare.

    Change your control to the following:

    <asp:RangeValidator ID="RangeValidator8" runat="server" ControlToValidate="TextBoxSAPPlannedHrs" 
        Display="None" ErrorMessage="Must be a valid number 0-999"  MaximumValue="999"
        MinimumValue="0" Type="Integer"></asp:RangeValidator>
    

    This will cause the input to be converted to the type specified (Integer) before doing the compare. If the conversion fails, the validation will fail.

    See MSDN:

    The values are implicitly converted to the specified data type before the comparison is made. If the data conversion fails, data validation fails.