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!"); }
精彩评论