You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2005/10/29 05:00:49 UTC

DO NOT REPLY [Bug 37300] New: - Int validation in Java and Javascript have different semantics

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=37300>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=37300

           Summary: Int validation in Java and Javascript have different
                    semantics
           Product: Commons
           Version: unspecified
          Platform: Other
        OS/Version: other
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Validator
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: chris@christopherschultz.net


If you run the string "09" through the Java integer validator
(org.apache.commons.validator.GenericTypeValidator.formatInt), it validates
successfully as an integer (as the number 9, actually).

If you run the same string through the Javascript integer validator
(org/apache/commons/validator/javascript/validateInteger.js), it signals an
error that the string is not a valid number.

The problem is that Javascript's parseInt function (used in the Javascript
integer validation function) does radix snooping and uses the (pretty standard)
"0x" prefix to indicate a hexidecimal and a leading "0" to incidate an octal
value. On the other hand, the Java version of the validator uses "new
Integer(String)" to create a new Integer object.

The JavaDoc for Integer.<init>(String) states:

"The string is converted to an int value in exactly the manner used by the
parseInt method for radix 10."

So, the Java version explicitly uses radix 10, while the Javascript version will
auto-detect the radix.

I'm not sure if I care which way this goes, but these two ought to be consistent.

To "fix" the Javascript version, one technique would be to trim-off leading
zeros (not sure what to do about "0xf00"). To "fix" the Java version, we should
use Integer.decode(String) instead of new Integer(String).

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org