Skip to content

Commit ada073e

Browse files
timmywilgibson042
authored andcommitted
Core: make isNumeric limited to strings and numbers
Fixes gh-2662 (cherry picked from commit 15ac848)
1 parent cf4092e commit ada073e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/core.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ jQuery.extend( {
217217

218218
isNumeric: function( obj ) {
219219

220-
// parseFloat NaNs numeric-cast false positives (null|true|false|"")
221-
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
222-
// subtraction forces infinities to NaN
223-
// adding 1 corrects loss of precision from parseFloat (#15100)
224-
var realStringObj = obj && obj.toString();
225-
return !jQuery.isArray( obj ) && ( realStringObj - parseFloat( realStringObj ) + 1 ) >= 0;
220+
// As of jQuery 3.0, isNumeric is limited to
221+
// strings and numbers (primitives or objects)
222+
// that can be coerced to finite numbers (gh-2662)
223+
var type = jQuery.type( obj );
224+
return ( type === "number" || type === "string" ) &&
225+
( obj - parseFloat( obj ) + 1 ) >= 0;
226226
},
227227

228228
isEmptyObject: function( obj ) {

test/unit/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ QUnit.test( "isNumeric", function( assert ) {
479479
assert.ok( t( 1.5999999999999999 ), "Very precise floating point number" );
480480
assert.ok( t( 8e5 ), "Exponential notation" );
481481
assert.ok( t( "123e-2" ), "Exponential notation string" );
482-
assert.ok( t( new ToString( "42" ) ), "Custom .toString returning number" );
483482

483+
assert.equal( t( new ToString( "42" ) ), false, "Custom .toString returning number" );
484484
assert.equal( t( "" ), false, "Empty string" );
485485
assert.equal( t( " " ), false, "Whitespace characters string" );
486486
assert.equal( t( "\t\t" ), false, "Tab characters string" );

0 commit comments

Comments
 (0)