]> source.dussan.org Git - jquery.git/commitdiff
Fix #10690. Make sure `.isNumeric()` can't get a `Date`.
authorDave Methvin <dave.methvin@gmail.com>
Mon, 7 Nov 2011 16:25:51 +0000 (11:25 -0500)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 7 Nov 2011 16:25:51 +0000 (11:25 -0500)
Also add tests for custom objects with a `.toString()` method.

src/core.js
test/unit/core.js

index d0eb612e6005e1eaf78810078d029d06066461d2..62141b986a9765b0ea117cc2b6a0befa3aa91f16 100644 (file)
@@ -485,7 +485,7 @@ jQuery.extend({
        },
 
        isNumeric: function( obj ) {
-               return obj != null && rdigit.test( obj ) && !isNaN( obj );
+               return !isNaN( parseFloat(obj) ) && isFinite( obj );
        },
 
        type: function( obj ) {
index 252f08d574b2455e90c2cc1be1fd88551a5b5546..dc909546b36ae252b19c5bc0eea2b59df03cec44 100644 (file)
@@ -455,9 +455,17 @@ test("isFunction", function() {
 });
 
 test( "isNumeric", function() {
-       expect( 33 );
-
-       var t = jQuery.isNumeric;
+       expect( 37 );
+
+       var t = jQuery.isNumeric,
+               Traditionalists = function(n) {
+                       this.value = n;
+                       this.toString = function(){
+                               return String(this.value);
+                       };
+               },
+               answer = new Traditionalists( "42" ),
+               rong = new Traditionalists( "Devo" );
 
        ok( t("-10"), "Negative integer string");
        ok( t("0"), "Zero string");
@@ -475,6 +483,7 @@ test( "isNumeric", function() {
        ok( t(3.1415), "Positive floating point number");
        ok( t(8e5), "Exponential notation");
        ok( t("123e-2"), "Exponential notation string");
+       ok( t(answer), "Custom .toString returning number");
        equal( t(""), false, "Empty string");
        equal( t("        "), false, "Whitespace characters string");
        equal( t("\t\t"), false, "Tab characters string");
@@ -490,8 +499,11 @@ test( "isNumeric", function() {
        equal( t(Infinity), false, "Infinity primitive");
        equal( t(Number.POSITIVE_INFINITY), false, "Positive Infinity");
        equal( t(Number.NEGATIVE_INFINITY), false, "Negative Infinity");
+       equal( t(rong), false, "Custom .toString returning non-number");
        equal( t({}), false, "Empty object");
        equal( t(function(){} ), false, "Instance of a function");
+       equal( t( new Date ), false, "Instance of a Date");
+       equal( t(function(){} ), false, "Instance of a function");
 });
 
 test("isXMLDoc - HTML", function() {