简体   繁体   English

如何为Int64创建RangeValidator? BigInt? 长?

[英]How can I create a RangeValidator for Int64? BigInt? Long?

So I'm trying to use the RangeValidator to ensure the minimum and maximum value of Int64 is validated. 因此,我试图使用RangeValidator来确保Int64的最小值和最大值得到验证。 But, the only or closest option that I seem to have is Double. 但是,我似乎唯一或最接近的选择是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: 例如,Int64的最大值是:

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. 我假设这是由于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. 我确实看到System.Configuration中有一个LongValidator,但是我试图尽可能避免创建CustomValidator类型,并且它是一个不同的验证类型,因为不打算像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. 也标记了C#。 Ignore my vb.net example.. not really important based on the question. 忽略我的vb.net示例。 Will convert any code either way. 将以任何一种方式转换任何代码。

I found a plausible answer on a forum by PLBlum 我在PLBlum论坛上找到了合理的答案

I want to clarify something. 我想澄清一下。 RangeValidator does not use a regularexpression to validate a double. RangeValidator不使用正则表达式来验证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. 然后,它使用javascript parseFloat()函数将结果转换为实际数字。 If that function cannot convert, the RangeValidator knows the value was not acceptable to javascript. 如果该函数无法转换,则RangeValidator知道javascript不接受该值。

I suspect that the problem is that the value you are assigning to MaximumValue is too large for javascript's floating point. 我怀疑问题在于您分配给MaximumValue的值对于javascript的浮点而言太大。 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. 我认为.net的Double和javascript都是80位浮点,但在最小和最大范围内可能略有不同。 In any case, I recommend changing your code like this: 无论如何,我建议像这样更改代码:

  • Use a CompareValidator, Operator=DataTypeCheck, Type=Double. 使用CompareValidator,Operator = DataTypeCheck,Type = Double。 It will report an illegal value due to the format or its too big for an 80 bit number. 由于格式错误或格式太大(对于80位数字),它将报告非法值。

  • Use another CompareValidator, Operator=GreaterThanEqual, Type=Double, ValueToCompare= your minimum double. 使用另一个CompareValidator,Operator = GreaterThanEqual,Type = Double,ValueToCompare =您的最小倍数。 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. 也许可以解释为什么它验证不正确。

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". System.configuration中有一个函数,该函数可以检查值是否为有效的“ 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. 否则,该数字太大,JavaScript会将其解析为浮点数,如Sam所述。

So, I ended up using a regex as to gain client-side and server-side validation. 因此,我最终使用了正则表达式来获得客户端和服务器端验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM