Trying to do front-end validation on an HTML input instead of throwing an exception in the Java back-end.
3 Answers
Check whether the number is more than 2147483647.
For example:
if (parseInt(num, 10) > 2147483647)
//BAD!!!
3 Comments
Jonathon Faust
Good -- might be worth mentioning that you must do input validation on the Java back end as well as the front end.
kennytm
@SLaks: Perhaps you need to check the lower bound (-2147483648) as well.
user267272
it is signed, so all of the above apply. why didn't I think of this?
if ((the_number >> 0) != the_number) {
// overflow...
}
You still need a server-side check because the client-side may turn off Javascript, etc.
3 Comments
Jonathon Faust
@KennyTM - if someone has only worked with JavaScript and Java, they might not be familiar with lower level operations like bit shifts -- could you flesh out an explanation of what you're doing? OP doesn't know enough to work out what 32-bit overflow would look like, so an explanation would probably help.
user267272
I am familiar with 32-bit overflow. does javascript explicitly use 32-bit integers, or is this open to implementation?
kennytm
@user: 11.7.2 The Signed Right Shift Operator: "The result is a signed 32-bit integer." (Ref: ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf)