Trying to do front-end validation on an html input instead of throwing an exception in the java back end.
views:
119answers:
3
+2
A:
Check whether the number is more than 2147483647
.
For example:
if (parseInt(num, 10) > 2147483647)
//BAD!!!
SLaks
2010-02-17 16:35:56
Good -- might be worth mentioning that you *must* do input validation on the Java back end as well as the front end.
Jonathon
2010-02-17 16:37:40
@SLaks: Perhaps you need to check the lower bound (-2147483648) as well.
KennyTM
2010-02-17 16:39:41
it is signed, so all of the above apply. why didn't I think of this?
2010-02-17 16:42:57
+1
A:
if ((the_number >> 0) != the_number) {
// overflow...
}
You still need a server-side check because the client-side may turn off Javascript, etc.
KennyTM
2010-02-17 16:36:46
@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.
Jonathon
2010-02-17 16:40:08
I am familiar with 32-bit overflow. does javascript explicitly use 32-bit integers, or is this open to implementation?
2010-02-17 16:50:26
@user: 11.7.2 The Signed Right Shift Operator: "The result is a signed 32-bit integer." (Ref: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf)
KennyTM
2010-02-17 16:56:31