开发者

Determine if conversion from string to 32-bit integer will overflow

开发者 https://www.devze.com 2022-12-20 16:45 出处:网络
Trying to do front-end validat开发者_开发技巧ion on an HTML input instead of throwing an exception in the Java back-end.Check whether the number is more than 2147483647.

Trying to do front-end validat开发者_开发技巧ion on an HTML input instead of throwing an exception in the Java back-end.


Check whether the number is more than 2147483647.

For example:

if (parseInt(num, 10) > 2147483647)
    //BAD!!!


if ((the_number >> 0) != the_number) {
  // overflow...
}

You still need a server-side check because the client-side may turn off Javascript, etc.


Just check:

if (parseInt(myNumberAsString, 10) > 2147483647) {  alert("Invalid int!"); }
0

精彩评论

暂无评论...
验证码 换一张
取 消