1

Is there a way to use RangeValidator for large numbers (Int64 range)?

nima
  • 6,566
  • 4
  • 45
  • 57

1 Answers1

3

After looking that this there is no clean way to do this with just a rangevalidator. I've listed the alternatives below that use the webforms validators.

HTML:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  <asp:RangeValidator ID="RangeValidator1" ControlToValidate="TextBox1"
runat="server" ErrorMessage="Help" SetFocusOnError="True" Type="Double"></asp:RangeValidator>

HTML Alt:

You could use a combo here. One to verify that its in the 64 bit range and one to validate its just an integer. Seems clunky, but it should work.

<asp:RangeValidator ID="RangeValidator1" ControlToValidate="TextBox1"
    runat="server" ErrorMessage="Help" SetFocusOnError="True" Type="Double"></asp:RangeValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
      ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator" 
      ValidationExpression="[1-9]\d*"></asp:RegularExpressionValidator>

Code Behind:

protected void Page_Load(object sender, EventArgs e) {
      this.RangeValidator1.MaximumValue = Int64.MaxValue.ToString();
    }
  • Yes and I get this message: The value '9223372036854775807' of the MaximumValue property of 'RangeValidator1' cannot be converted to type 'Integer' – nima Apr 04 '11 at 13:05
  • Can you show me more of your code/html? I setup a quick example project with a textbox and a rangevalidator and everything worked as expected. –  Apr 04 '11 at 17:26
  • Thanks for your ripely. Your sample works because RangeValidator's default type is "String" so you can basically assign any string to Maximum value. But the problem is if you use this validator for validating numbers, for example "93" wont validate because it is larger than Int64.MaxValue ("9223372036854775807") string-wise. – nima Apr 04 '11 at 21:26
  • Change the type value to a double. This will obviously allow 64 bit floating points, but not for sure about your requirements. Edited code above. –  Apr 05 '11 at 01:25
  • Thanks for the solution, But there is one tiny problem. With two validators, one asking the user to input a number and the other checking the range, if the user enters a non-numeric character the message will be shown far from the textbox because a space is preserved for the first validator. Is there a way to shrink validaros when they have no message? – nima Apr 05 '11 at 19:30
  • The HTML generated by those controls can be styled with CSS anyway you like. They generate elements that CSS can style. To simplify CSS, add a CssClass="whateverClassYouLike" to each validator. –  Apr 05 '11 at 20:02
  • By default, the visibility style is set to hidden on a valid validator, meaning that the element is not shown on the screen, but the layout is still effected. By adding `Display="Dynamic"` to the validator, it uses `display:none` to hide the validator when it's valid instead. – ajbeaven Apr 16 '12 at 01:52