开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜