0

So I'm trying to use the RangeValidator to ensure the minimum and maximum value of Int64 is validated. But, the only or closest option that I seem to have is Double.

Dim Range As New RangeValidator
Range.ControlToValidate = "..."
Range.Type = ValidationDataType.Double
Range.MinValue = Int64.MinValue.ToString
Range.MaxValue = Int64.MaxValue.ToString

I don't want to use a custom validator. I suppose I could use some crazy regular expression.. but just trying to understand why I can't do this.

For example, the max value of Int64 is:

9223372036854775807

But this validates:

9223372036854775808
9223372036854775810
9223372036854775899

It will not validate if I jump up a larger number:

9223372036854799999

I'm assuming it's due to some conversion taking place for a Double.

I do see there's a LongValidator in System.Configuration, but I'm trying to avoid creating a CustomValidator type if at all possible and it's a different validate type, as in not intended for use like RangeValidator.

All of this is more so for learning purposes than anything else. I'm aware I can jump through other hoops but hoping to get better clarity.

Tagged C# as well. Ignore my vb.net example.. not really important based on the question. Will convert any code either way.

user1447679
  • 3,076
  • 7
  • 32
  • 69
  • maybe the same question as [using-asp-net-rangevalidator-for-int64](http://stackoverflow.com/questions/5537971/using-asp-net-rangevalidator-for-int64) ? – Sam Apr 14 '16 at 23:18
  • @Sam Saw that one, only I don't think they actually ran into the problem that I did. They ended up using a regex and a range, but it's still a problem with the Double type. – user1447679 Apr 14 '16 at 23:19
  • :) fair enough, I'm afraid I'm not savvy enough as to why it's validating but I'd assume you're on the right track with the double type .. – Sam Apr 14 '16 at 23:22
  • @Sam It's driving me crazy. I just wish Microsoft would add more data types in the validators. :) – user1447679 Apr 14 '16 at 23:30

2 Answers2

0

I found a plausible answer on a forum by PLBlum

I want to clarify something. RangeValidator does not use a regularexpression to validate a double. It uses a regular expression to extract the digits and decimal character out of the string. It then converts the result into an actual number using the javascript parseFloat() function. If that function cannot convert, the RangeValidator knows the value was not acceptable to javascript.

I suspect that the problem is that the value you are assigning to MaximumValue is too large for javascript's floating point. I think both .net's Double and javascript are 80bit floating point but its possible they are slightly different at the min and max range. In any case, I recommend changing your code like this:

  • Use a CompareValidator, Operator=DataTypeCheck, Type=Double. It will report an illegal value due to the format or its too big for an 80 bit number.

  • Use another CompareValidator, Operator=GreaterThanEqual, Type=Double, ValueToCompare= your minimum double. This will report an error if anything is below the minimum. The max was handled in the other validator.

Maybe that explains why it is validating incorrectly.

Sam
  • 1,634
  • 13
  • 21
  • Interesting approach, and I very much so appreciate you helping me research. I think the problem still stands, because I don't want to allow decimal places, and your suggested workaround would do exactly that. I can always use a custom validator... I'm just stumped as to why there isn't another way. Thanks again @Sam. – user1447679 Apr 15 '16 at 00:00
  • Know what... this sparks a thought. Gonna try something. – user1447679 Apr 15 '16 at 00:07
  • Nope didn't work. Disabled the client-side validation and it still failed, so back to the double issue. – user1447679 Apr 15 '16 at 00:08
  • At this point I'd just go with a custom solution really, in my opinion even if there's a standardised way it's obviously not very well documented, loosing more time researching it over actually writing it yourself and move on.. :) we are programmers after all, right? :D – Sam Apr 16 '16 at 06:03
  • I went with a regex. I ended up with a migraine after constructing it, but it works. All of this was more for research/learning anyway, and I do appreciate your help and ideas. – user1447679 Apr 17 '16 at 21:15
0

Unless someone else wants to chime in, realistically there's one way I found to do this, and that's with a custom validator. There's a function in System.configuration that allows the ability to check if a value is a valid "Long". The problem is, if you want client-side validation it seems the only valid option is to use a regex. Otherwise, the number is just too big and JavaScript parses it as a float, as Sam mentioned.

So, I ended up using a regex as to gain client-side and server-side validation.

user1447679
  • 3,076
  • 7
  • 32
  • 69