<p>What's the cleanest, most effective way to validate decimal numbers in JavaScript?</p>

<p>Bonus points for:</p>

<ol>
<li>Clarity. Solution should be clean and simple.</li>
<li>Cross-platform.</li>
</ol>

<p>Test cases:</p>

<pre><code>01. IsNumeric('-1')      =&gt; true
02. IsNumeric('-1.5')    =&gt; true
03. IsNumeric('0')       =&gt; true
04. IsNumeric('0.42')    =&gt; true
05. IsNumeric('.42')     =&gt; true
06. IsNumeric('99,999')  =&gt; false
07. IsNumeric('0x89f')   =&gt; false
08. IsNumeric('#abcdef') =&gt; false
09. IsNumeric('1.2.3')   =&gt; false
10. IsNumeric('')        =&gt; false
11. IsNumeric('blah')    =&gt; false
</code></pre>

[{'answer_id': 18085, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 6, 'selected': False, 'text': '<p>This way seems to work well:</p>\n\n<pre><code>function IsNumeric(input){\n    var RE = /^-{0,1}\\d*\\.{0,1}\\d+$/;\n    return (RE.test(input));\n}\n</code></pre>\n\n<p>In one line:</p>\n\n<pre><code>const IsNumeric = (num) =&gt; /^-{0,1}\\d*\\.{0,1}\\d+$/.test(num);\n</code></pre>\n\n<p>And to test it:</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const IsNumeric = (num) =&gt; /^-{0,1}\\d*\\.{0,1}\\d+$/.test(num);\r\n    \r\n    function TestIsNumeric(){\r\n        var results = \'\'\r\n        results += (IsNumeric(\'-1\')?"Pass":"Fail") + ": IsNumeric(\'-1\') =&gt; true\\n";\r\n        results += (IsNumeric(\'-1.5\')?"Pass":"Fail") + ": IsNumeric(\'-1.5\') =&gt; true\\n";\r\n        results += (IsNumeric(\'0\')?"Pass":"Fail") + ": IsNumeric(\'0\') =&gt; true\\n";\r\n        results += (IsNumeric(\'0.42\')?"Pass":"Fail") + ": IsNumeric(\'0.42\') =&gt; true\\n";\r\n        results += (IsNumeric(\'.42\')?"Pass":"Fail") + ": IsNumeric(\'.42\') =&gt; true\\n";\r\n        results += (!IsNumeric(\'99,999\')?"Pass":"Fail") + ": IsNumeric(\'99,999\') =&gt; false\\n";\r\n        results += (!IsNumeric(\'0x89f\')?"Pass":"Fail") + ": IsNumeric(\'0x89f\') =&gt; false\\n";\r\n        results += (!IsNumeric(\'#abcdef\')?"Pass":"Fail") + ": IsNumeric(\'#abcdef\') =&gt; false\\n";\r\n        results += (!IsNumeric(\'1.2.3\')?"Pass":"Fail") + ": IsNumeric(\'1.2.3\') =&gt; false\\n";\r\n        results += (!IsNumeric(\'\')?"Pass":"Fail") + ": IsNumeric(\'\') =&gt; false\\n";\r\n        results += (!IsNumeric(\'blah\')?"Pass":"Fail") + ": IsNumeric(\'blah\') =&gt; false\\n";\r\n        \r\n        return results;\r\n    }\r\n\r\nconsole.log(TestIsNumeric());</code></pre>\r\n<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p>I borrowed that regex from <a href="http://www.codetoad.com/javascript/isnumeric.asp" rel="noreferrer">http://www.codetoad.com/javascript/isnumeric.asp</a>. Explanation:</p>\n\n<pre><code>/^ match beginning of string\n-{0,1} optional negative sign\n\\d* optional digits\n\\.{0,1} optional decimal point\n\\d+ at least one digit\n$/ match end of string\n</code></pre>\n'}, {'answer_id': 18148, 'author': 'pottedmeat', 'author_id': 2120, 'author_profile': 'https://Stackoverflow.com/users/2120', 'pm_score': 3, 'selected': False, 'text': "<p>A couple of tests to add:</p>\n\n<pre><code>IsNumeric('01.05') =&gt; false\nIsNumeric('1.') =&gt; false\nIsNumeric('.') =&gt; false\n</code></pre>\n\n<p>I came up with this:</p>\n\n<pre><code>function IsNumeric(input) {\n    return /^-?(0|[1-9]\\d*|(?=\\.))(\\.\\d+)?$/.test(input);\n}\n</code></pre>\n\n<p>The solution covers:</p>\n\n<ul>\n<li>An optional negative sign at the beginning</li>\n<li>A single zero, or one or more digits not starting with 0, or nothing so long as a period follows</li>\n<li>A period that is followed by 1 or more numbers</li>\n</ul>\n"}, {'answer_id': 18249, 'author': 'Marius', 'author_id': 1585, 'author_profile': 'https://Stackoverflow.com/users/1585', 'pm_score': 3, 'selected': False, 'text': "<p>I'd like to add the following:</p>\n\n<pre>\n<code>1. IsNumeric('0x89f') => true\n2. IsNumeric('075') => true</code>\n</pre>\n\n<p>Positive hex numbers start with <code>0x</code> and negative hex numbers start with <code>-0x</code>.\nPositive oct numbers start with <code>0</code> and negative oct numbers start with <code>-0</code>.\nThis one takes most of what has already been mentioned into consideration, but includes hex and octal numbers, negative scientific, Infinity and has removed decimal scientific (<code>4e3.2</code> is not valid).</p>\n\n<pre><code>function IsNumeric(input){\n  var RE = /^-?(0|INF|(0[1-7][0-7]*)|(0x[0-9a-fA-F]+)|((0|[1-9][0-9]*|(?=[\\.,]))([\\.,][0-9]+)?([eE]-?\\d+)?))$/;\n  return (RE.test(input));\n}\n</code></pre>\n"}, {'answer_id': 19176, 'author': 'bubbassauro', 'author_id': 1328, 'author_profile': 'https://Stackoverflow.com/users/1328', 'pm_score': 4, 'selected': False, 'text': '<p>Use the function <code>isNaN</code>. I believe if you test for <code>!isNaN(yourstringhere)</code> it works fine for any of these situations.</p>\n'}, {'answer_id': 19317, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 5, 'selected': False, 'text': '<p>Yeah, the built-in <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN" rel="noreferrer"><code>isNaN(object)</code></a> will be much faster than any regex parsing, because it\'s built-in and compiled, instead of interpreted on the fly.</p>\n\n<p>Although the results are somewhat different to what you\'re looking for (<a href="http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_isnan" rel="noreferrer">try it</a>):</p>\n\n<pre><code>                                              // IS NUMERIC\ndocument.write(!isNaN(\'-1\') + "&lt;br /&gt;");      // true\ndocument.write(!isNaN(\'-1.5\') + "&lt;br /&gt;");    // true\ndocument.write(!isNaN(\'0\') + "&lt;br /&gt;");       // true\ndocument.write(!isNaN(\'0.42\') + "&lt;br /&gt;");    // true\ndocument.write(!isNaN(\'.42\') + "&lt;br /&gt;");     // true\ndocument.write(!isNaN(\'99,999\') + "&lt;br /&gt;");  // false\ndocument.write(!isNaN(\'0x89f\') + "&lt;br /&gt;");   // true\ndocument.write(!isNaN(\'#abcdef\') + "&lt;br /&gt;"); // false\ndocument.write(!isNaN(\'1.2.3\') + "&lt;br /&gt;");   // false\ndocument.write(!isNaN(\'\') + "&lt;br /&gt;");        // true\ndocument.write(!isNaN(\'blah\') + "&lt;br /&gt;");    // false\n</code></pre>\n'}, {'answer_id': 22604, 'author': 'Aquatic', 'author_id': 2080, 'author_profile': 'https://Stackoverflow.com/users/2080', 'pm_score': 4, 'selected': False, 'text': '<p>It can be done without RegExp as </p>\n\n<pre><code>function IsNumeric(data){\n    return parseFloat(data)==data;\n}\n</code></pre>\n'}, {'answer_id': 174921, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 8, 'selected': False, 'text': "<p>Arrrgh!  Don't listen to the regular expression answers.  RegEx is icky for this, and I'm not talking just performance.  It's so easy to make subtle, impossible to spot mistakes with your regular expression.  </p>\n\n<p>If you can't use <code>isNaN()</code>, this should work much better:</p>\n\n<pre><code>function IsNumeric(input)\n{\n    return (input - 0) == input &amp;&amp; (''+input).trim().length &gt; 0;\n}\n</code></pre>\n\n<p>Here's how it works:</p>\n\n<p>The <code>(input - 0)</code> expression forces JavaScript to do type coercion on your input value; it must first be interpreted as a number for the subtraction operation.  If that conversion to a number fails, the expression will result in <code>NaN</code>.  This <em>numeric</em> result is then compared to the original value you passed in.  Since the left hand side is now numeric, type coercion is again used.  Now that the input from both sides was coerced to the same type from the same original value, you would think they should always be the same (always true). However, there's a special rule that says <code>NaN</code> is never equal to <code>NaN</code>, and so a value that can't be converted to a number (and only values that cannot be converted to numbers) will result in false. </p>\n\n<p>The check on the length is for a special case involving empty strings. Also note that it falls down on your 0x89f test, but that's because in many environments that's an okay way to define a number literal.  If you want to catch that specific scenario you could add an additional check.  Even better, if that's your reason for not using <code>isNaN()</code> then just wrap your own function around <code>isNaN()</code> that can also do the additional check.</p>\n\n<p>In summary, <strong><em>if you want to know if a value can be converted to a number, actually try to convert it to a number.</em></strong></p>\n\n<hr>\n\n<p>I went back and did some research for <em>why</em> a whitespace string did not have the expected output, and I think I get it now: an empty string is coerced to <code>0</code> rather than <code>NaN</code>. Simply trimming the string before the length check will handle this case.</p>\n\n<p>Running the unit tests against the new code and it only fails on the infinity and boolean literals, and the only time that should be a problem is if you're generating code (really, who would type in a literal and check if it's numeric? You should <em>know</em>), and that would be some strange code to generate.</p>\n\n<p>But, again, <strong>the only reason ever to use this is if for some reason you have to avoid isNaN().</strong></p>\n"}, {'answer_id': 1280236, 'author': 'camomileCase', 'author_id': 143145, 'author_profile': 'https://Stackoverflow.com/users/143145', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://en.wikipedia.org/wiki/Yahoo!_UI_Library" rel="noreferrer">Yahoo! UI</a> uses this:</p>\n\n<pre><code>isNumber: function(o) {\n    return typeof o === \'number\' &amp;&amp; isFinite(o);\n}\n</code></pre>\n'}, {'answer_id': 1561597, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': False, 'text': '<pre><code>function IsNumeric(num) {\n     return (num &gt;=0 || num &lt; 0);\n}\n</code></pre>\n\n<p>This works for 0x23 type numbers as well.</p>\n'}, {'answer_id': 1830844, 'author': 'Christian C. Salvadó', 'author_id': 5445, 'author_profile': 'https://Stackoverflow.com/users/5445', 'pm_score': 13, 'selected': True, 'text': '<p><a href="https://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/174921#174921">@Joel\'s answer</a> is pretty close, but it will fail in the following cases:</p>\n\n<pre><code>// Whitespace strings:\nIsNumeric(\' \')    == true;\nIsNumeric(\'\\t\\t\') == true;\nIsNumeric(\'\\n\\r\') == true;\n\n// Number literals:\nIsNumeric(-1)  == false;\nIsNumeric(0)   == false;\nIsNumeric(1.1) == false;\nIsNumeric(8e5) == false;\n</code></pre>\n\n<p>Some time ago I had to implement an <code>IsNumeric</code> function, to find out if a variable contained a numeric value, <strong>regardless of its type</strong>, it could be a <code>String</code> containing a numeric value (I had to consider also exponential notation, etc.), a <code>Number</code> object, virtually anything could be passed to that function, I couldn\'t make any type assumptions,  taking care of type coercion (eg. <code>+true == 1;</code> but <code>true</code> shouldn\'t be considered as <code>"numeric"</code>).</p>\n\n<p>I think is worth sharing this set of <a href="http://run.plnkr.co/plunks/93FPpacuIcXqqKMecLdk/" rel="noreferrer"><strong>+30 unit tests</strong></a> made to numerous function implementations, and also share the one that passes all my tests:</p>\n\n<pre><code>function isNumeric(n) {\n    return !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\n}\n</code></pre>\n\n<p><strong>P.S.</strong> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN" rel="noreferrer">isNaN</a> &amp; <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite" rel="noreferrer">isFinite</a> have a confusing behavior due to forced conversion to number. In ES6, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN" rel="noreferrer">Number.isNaN</a> &amp; <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite" rel="noreferrer">Number.isFinite</a> would fix these issues. Keep that in mind when using them. </p>\n\n<hr>\n\n<p><strong>Update</strong> : \n<a href="https://github.com/jquery/jquery/blob/2.2-stable/src/core.js#L215" rel="noreferrer">Here\'s how jQuery does it now (2.2-stable)</a>: </p>\n\n<pre><code>isNumeric: function(obj) {\n    var realStringObj = obj &amp;&amp; obj.toString();\n    return !jQuery.isArray(obj) &amp;&amp; (realStringObj - parseFloat(realStringObj) + 1) &gt;= 0;\n}\n</code></pre>\n\n<p><strong>Update</strong> :\n<a href="https://github.com/angular/angular/blob/4.3.x/packages/common/src/pipes/number_pipe.ts#L172" rel="noreferrer">Angular 4.3</a>:</p>\n\n<pre><code>export function isNumeric(value: any): boolean {\n    return !isNaN(value - parseFloat(value));\n}\n</code></pre>\n'}, {'answer_id': 2928538, 'author': 'InsertNameHere', 'author_id': 352835, 'author_profile': 'https://Stackoverflow.com/users/352835', 'pm_score': 3, 'selected': False, 'text': "<p>To me, this is the best way:</p>\n\n<pre><code>isNumber : function(v){\n   return typeof v === 'number' &amp;&amp; isFinite(v);\n}\n</code></pre>\n"}, {'answer_id': 4365908, 'author': 'user532188', 'author_id': 532188, 'author_profile': 'https://Stackoverflow.com/users/532188', 'pm_score': 2, 'selected': False, 'text': "<p>This should work. Some of the functions provided here are flawed, also should be faster than any other function here.</p>\n\n<pre><code>        function isNumeric(n)\n        {\n            var n2 = n;\n            n = parseFloat(n);\n            return (n!='NaN' &amp;&amp; n2==n);\n        }\n</code></pre>\n\n<p>Explained:</p>\n\n<p>Create a copy of itself, then converts the number into float, then compares itself with the original number, if it is still a number, (whether integer or float) , and matches the original number, that means, it is indeed a number.</p>\n\n<p>It works with numeric strings as well as plain numbers. Does not work with hexadecimal numbers. </p>\n\n<p>Warning: use at your own risk, no guarantees.</p>\n"}, {'answer_id': 4674364, 'author': 'jberenguer', 'author_id': 573156, 'author_profile': 'https://Stackoverflow.com/users/573156', 'pm_score': -1, 'selected': False, 'text': '<p>The following may work as well.</p>\n\n<pre><code>function isNumeric(v) {\n         return v.length &gt; 0 &amp;&amp; !isNaN(v) &amp;&amp; v.search(/[A-Z]|[#]/ig) == -1;\n   };\n</code></pre>\n'}, {'answer_id': 4827657, 'author': 'jayakumar', 'author_id': 593720, 'author_profile': 'https://Stackoverflow.com/users/593720', 'pm_score': 3, 'selected': False, 'text': "<pre><code>return (input - 0) == input &amp;&amp; input.length &gt; 0;\n</code></pre>\n\n<p>didn't work for me. When I put in an alert and tested, <code>input.length</code> was <code>undefined</code>. I think there is no property to check integer length. So what I did was</p>\n\n<pre><code>var temp = '' + input;\nreturn (input - 0) == input &amp;&amp; temp.length &gt; 0;\n</code></pre>\n\n<p>It worked fine.</p>\n"}, {'answer_id': 4975201, 'author': 'Manusoftar', 'author_id': 613785, 'author_profile': 'https://Stackoverflow.com/users/613785', 'pm_score': 2, 'selected': False, 'text': '<p>My solution,</p>\n\n<pre><code>function isNumeric(input) {\n    var number = /^\\-{0,1}(?:[0-9]+){0,1}(?:\\.[0-9]+){0,1}$/i;\n    var regex = RegExp(number);\n    return regex.test(input) &amp;&amp; input.length&gt;0;\n}\n</code></pre>\n\n<p>It appears to work in every situation, but I might be wrong.</p>\n'}, {'answer_id': 6306344, 'author': 'solidarius', 'author_id': 792721, 'author_profile': 'https://Stackoverflow.com/users/792721', 'pm_score': 2, 'selected': False, 'text': '<p>An integer value can be verified by:</p>\n\n<pre><code>function isNumeric(value) {\n    var bool = isNaN(+value));\n    bool = bool || (value.indexOf(\'.\') != -1);\n    bool = bool || (value.indexOf(",") != -1);\n    return !bool;\n};\n</code></pre>\n\n<p>This way is easier and faster! All tests are checked!</p>\n'}, {'answer_id': 7349746, 'author': 'Doctor Rudolf', 'author_id': 563688, 'author_profile': 'https://Stackoverflow.com/users/563688', 'pm_score': -1, 'selected': False, 'text': '<p>@Zoltan Lengyel \'other locales\' comment (Apr 26 at 2:14) in @CMS Dec answer (2 \'09 at 5:36):</p>\n\n<p>I would recommend testing for <code>typeof (n) === \'string\'</code>:</p>\n\n<pre><code>    function isNumber(n) {\n        if (typeof (n) === \'string\') {\n            n = n.replace(/,/, ".");\n        }\n        return !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\n    }\n</code></pre>\n\n<p>This extends Zoltans recommendation to not only be able to test "localized numbers" like  <code>isNumber(\'12,50\')</code> but also "pure" numbers like <code>isNumber(2011)</code>.</p>\n'}, {'answer_id': 9776221, 'author': 'Rafael', 'author_id': 1279325, 'author_profile': 'https://Stackoverflow.com/users/1279325', 'pm_score': -1, 'selected': False, 'text': "<p>Well, I'm using this one I made...</p>\n\n<p>It's been working so far:</p>\n\n<pre><code>function checkNumber(value) {\n    if ( value % 1 == 0 )\n        return true;\n    else\n        return false;\n}\n</code></pre>\n\n<p>If you spot any problem with it, tell me, please.</p>\n\n<p>Like any numbers should be divisible by one with nothing left, I figured I could just use the module, and if you try dividing a string into a number the result wouldn't be that. So.</p>\n"}, {'answer_id': 10992737, 'author': 'Hans Schmucker', 'author_id': 1450658, 'author_profile': 'https://Stackoverflow.com/users/1450658', 'pm_score': 3, 'selected': False, 'text': "<p>If I'm not mistaken, this should match any valid JavaScript number value, excluding constants (<code>Infinity</code>, <code>NaN</code>) and the sign operators <code>+</code>/<code>-</code> (because they are not actually part of the number as far as I concerned, they are separate operators):</p>\n\n<p>I needed this for a tokenizer, where sending the number to JavaScript for evaluation wasn't an option... It's definitely not the shortest possible regular expression, but I believe it catches all the finer subtleties of JavaScript's number syntax.</p>\n\n<pre><code>/^(?:(?:(?:[1-9]\\d*|\\d)\\.\\d*|(?:[1-9]\\d*|\\d)?\\.\\d+|(?:[1-9]\\d*|\\d)) \n(?:[e]\\d+)?|0[0-7]+|0x[0-9a-f]+)$/i\n</code></pre>\n\n<p><strong>Valid numbers would include:</strong></p>\n\n<pre><code> - 0\n - 00\n - 01\n - 10\n - 0e1\n - 0e01\n - .0\n - 0.\n - .0e1\n - 0.e1\n - 0.e00\n - 0xf\n - 0Xf\n</code></pre>\n\n<p><strong>Invalid numbers would be</strong></p>\n\n<pre><code> - 00e1\n - 01e1\n - 00.0\n - 00x0\n - .\n - .e0\n</code></pre>\n"}, {'answer_id': 11063402, 'author': 'Ali Gonabadi', 'author_id': 1016287, 'author_profile': 'https://Stackoverflow.com/users/1016287', 'pm_score': 2, 'selected': False, 'text': "<p>I'm using simpler solution:</p>\n\n<pre><code>function isNumber(num) {\n    return parseFloat(num).toString() == num\n}\n</code></pre>\n"}, {'answer_id': 13618756, 'author': 'bob', 'author_id': 1088866, 'author_profile': 'https://Stackoverflow.com/users/1088866', 'pm_score': -1, 'selected': False, 'text': '<p>Here I\'ve collected the "good ones" from this page and put them into a simple test pattern for you to evaluate on your own.</p>\n\n<p>For newbies, the <code>console.log</code> is a built in function (available in all modern browsers) that lets you output results to the JavaScript console (dig around, you\'ll find it) rather than having to output to your HTML page.</p>\n\n<pre><code>var isNumeric = function(val){\n    // --------------------------\n    // Recommended\n    // --------------------------\n\n    // jQuery - works rather well\n    // See CMS\'s unit test also: http://dl.getdropbox.com/u/35146/js/tests/isNumber.html\n    return !isNaN(parseFloat(val)) &amp;&amp; isFinite(val);\n\n    // Aquatic - good and fast, fails the "0x89f" test, but that test is questionable.\n    //return parseFloat(val)==val;\n\n    // --------------------------\n    // Other quirky options\n    // --------------------------\n    // Fails on "", null, newline, tab negative.\n    //return !isNaN(val);\n\n    // user532188 - fails on "0x89f"\n    //var n2 = val;\n    //val = parseFloat(val);\n    //return (val!=\'NaN\' &amp;&amp; n2==val);\n\n    // Rafael - fails on negative + decimal numbers, may be good for isInt()?\n    // return ( val % 1 == 0 ) ? true : false;\n\n    // pottedmeat - good, but fails on stringy numbers, which may be a good thing for some folks?\n    //return /^-?(0|[1-9]\\d*|(?=\\.))(\\.\\d+)?$/.test(val);\n\n    // Haren - passes all\n    // borrowed from http://www.codetoad.com/javascript/isnumeric.asp\n    //var RE = /^-{0,1}\\d*\\.{0,1}\\d+$/;\n    //return RE.test(val);\n\n    // YUI - good for strict adherance to number type. Doesn\'t let stringy numbers through.\n    //return typeof val === \'number\' &amp;&amp; isFinite(val);\n\n    // user189277 - fails on "" and "\\n"\n    //return ( val &gt;=0 || val &lt; 0);\n}\n\nvar tests = [0, 1, "0", 0x0, 0x000, "0000", "0x89f", 8e5, 0x23, -0, 0.0, "1.0", 1.0, -1.5, 0.42, \'075\', "01", \'-01\', "0.", ".0", "a", "a2", true, false, "#000", \'1.2.3\', \'#abcdef\', \'\', "", "\\n", "\\t", \'-\', null, undefined];\n\nfor (var i=0; i&lt;tests.length; i++){\n    console.log( "test " + i + ":    " + tests[i] + "    \\t   " + isNumeric(tests[i]) );\n}\n</code></pre>\n'}, {'answer_id': 14932605, 'author': 'Kuf', 'author_id': 1393862, 'author_profile': 'https://Stackoverflow.com/users/1393862', 'pm_score': 4, 'selected': False, 'text': '<p>Since jQuery 1.7, you can use <a href="http://api.jquery.com/jQuery.isNumeric/"><code>jQuery.isNumeric()</code></a>:</p>\n\n<pre><code>$.isNumeric(\'-1\');      // true\n$.isNumeric(\'-1.5\');    // true\n$.isNumeric(\'0\');       // true\n$.isNumeric(\'0.42\');    // true\n$.isNumeric(\'.42\');     // true\n$.isNumeric(\'0x89f\');   // true (valid hexa number)\n$.isNumeric(\'99,999\');  // false\n$.isNumeric(\'#abcdef\'); // false\n$.isNumeric(\'1.2.3\');   // false\n$.isNumeric(\'\');        // false\n$.isNumeric(\'blah\');    // false\n</code></pre>\n\n<p>Just note that unlike what you said, <code>0x89f</code> is a valid number (hexa)</p>\n'}, {'answer_id': 15043984, 'author': 'Xotic750', 'author_id': 592253, 'author_profile': 'https://Stackoverflow.com/users/592253', 'pm_score': 6, 'selected': False, 'text': '<p>The accepted answer failed your test #7 and I guess it\'s because you changed your mind. So this is a response to the accepted answer, with which I had issues.</p>\n\n<p>During some projects I have needed to validate some data and be as certain as possible that it is a javascript numerical value that can be used in mathematical operations.</p>\n\n<p>jQuery, and some other javascript libraries already include such a function, usually called <code>isNumeric</code>. There is also a <a href="https://stackoverflow.com/a/1830844/592253">post on stackoverflow</a> that has been widely accepted as the answer, the same general routine that the afore mentioned libraries are using.</p>\n\n<pre><code>function isNumber(n) {\n  return !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\n}\n</code></pre>\n\n<p>First, the code above would return true if the argument was an array of length 1, and that single element was of a type deemed as numeric by the above logic. In my opinion, if it\'s an array then its not numeric.</p>\n\n<p>To alleviate this problem, I added a check to discount arrays from the logic</p>\n\n<pre><code>function isNumber(n) {\n  return Object.prototype.toString.call(n) !== \'[object Array]\' &amp;&amp;!isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\n}\n</code></pre>\n\n<p>Of course, you could also use <code>Array.isArray</code>, jquery <code>$.isArray</code> or prototype <code>Object.isArray</code> instead of <code>Object.prototype.toString.call(n) !== \'[object Array]\'</code></p>\n\n<p>My second issue was that Negative Hexadecimal integer literal strings ("-0xA" -> -10) were not being counted as numeric. However, Positive Hexadecimal integer literal strings ("0xA" -> 10) were treated as numeric.\nI needed both to be valid numeric.</p>\n\n<p>I then modified the logic to take this into account.</p>\n\n<pre><code>function isNumber(n) {\n  return Object.prototype.toString.call(n) !== \'[object Array]\' &amp;&amp;!isNaN(parseFloat(n)) &amp;&amp; isFinite(n.toString().replace(/^-/, \'\'));\n}\n</code></pre>\n\n<p>If you are worried about the creation of the regex each time the function is called then you could rewrite it within a closure, something like this</p>\n\n<pre><code>var isNumber = (function () {\n  var rx = /^-/;\n\n  return function (n) {\n      return Object.prototype.toString.call(n) !== \'[object Array]\' &amp;&amp;!isNaN(parseFloat(n)) &amp;&amp; isFinite(n.toString().replace(rx, \'\'));\n  };\n}());\n</code></pre>\n\n<p>I then took CMSs <a href="http://dl.getdropbox.com/u/35146/js/tests/isNumber.html" rel="noreferrer">+30 test cases</a> and cloned the <a href="http://jsfiddle.net/Xotic750/2q8pp/" rel="noreferrer">testing on jsfiddle</a> added my extra test cases and my above described solution.</p>\n\n<p>It may not replace the widely accepted/used answer but if this is more of what you are expecting as results from your isNumeric function then hopefully this will be of some help.</p>\n\n<p><strong>EDIT:</strong> As pointed out by <a href="https://stackoverflow.com/a/15230431/592253">Bergi</a>, there are other possible objects that could be considered numeric and it would be better to whitelist than blacklist. With this in mind I would add to the criteria.</p>\n\n<p>I want my isNumeric function to consider only Numbers or Strings</p>\n\n<p>With this in mind, it would be better to use</p>\n\n<pre><code>function isNumber(n) {\n  return (Object.prototype.toString.call(n) === \'[object Number]\' || Object.prototype.toString.call(n) === \'[object String]\') &amp;&amp;!isNaN(parseFloat(n)) &amp;&amp; isFinite(n.toString().replace(/^-/, \'\'));\n}\n</code></pre>\n\n<p><strong>Test the solutions</strong></p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>var testHelper = function() {\r\n\r\n  var testSuite = function() {\r\n    test("Integer Literals", function() {\r\n      ok(isNumber("-10"), "Negative integer string");\r\n      ok(isNumber("0"), "Zero string");\r\n      ok(isNumber("5"), "Positive integer string");\r\n      ok(isNumber(-16), "Negative integer number");\r\n      ok(isNumber(0), "Zero integer number");\r\n      ok(isNumber(32), "Positive integer number");\r\n      ok(isNumber("040"), "Octal integer literal string");\r\n      ok(isNumber(0144), "Octal integer literal");\r\n      ok(isNumber("-040"), "Negative Octal integer literal string");\r\n      ok(isNumber(-0144), "Negative Octal integer literal");\r\n      ok(isNumber("0xFF"), "Hexadecimal integer literal string");\r\n      ok(isNumber(0xFFF), "Hexadecimal integer literal");\r\n      ok(isNumber("-0xFF"), "Negative Hexadecimal integer literal string");\r\n      ok(isNumber(-0xFFF), "Negative Hexadecimal integer literal");\r\n    });\r\n\r\n    test("Foating-Point Literals", function() {\r\n      ok(isNumber("-1.6"), "Negative floating point string");\r\n      ok(isNumber("4.536"), "Positive floating point string");\r\n      ok(isNumber(-2.6), "Negative floating point number");\r\n      ok(isNumber(3.1415), "Positive floating point number");\r\n      ok(isNumber(8e5), "Exponential notation");\r\n      ok(isNumber("123e-2"), "Exponential notation string");\r\n    });\r\n\r\n    test("Non-Numeric values", function() {\r\n      equals(isNumber(""), false, "Empty string");\r\n      equals(isNumber("        "), false, "Whitespace characters string");\r\n      equals(isNumber("\\t\\t"), false, "Tab characters string");\r\n      equals(isNumber("abcdefghijklm1234567890"), false, "Alphanumeric character string");\r\n      equals(isNumber("xabcdefx"), false, "Non-numeric character string");\r\n      equals(isNumber(true), false, "Boolean true literal");\r\n      equals(isNumber(false), false, "Boolean false literal");\r\n      equals(isNumber("bcfed5.2"), false, "Number with preceding non-numeric characters");\r\n      equals(isNumber("7.2acdgs"), false, "Number with trailling non-numeric characters");\r\n      equals(isNumber(undefined), false, "Undefined value");\r\n      equals(isNumber(null), false, "Null value");\r\n      equals(isNumber(NaN), false, "NaN value");\r\n      equals(isNumber(Infinity), false, "Infinity primitive");\r\n      equals(isNumber(Number.POSITIVE_INFINITY), false, "Positive Infinity");\r\n      equals(isNumber(Number.NEGATIVE_INFINITY), false, "Negative Infinity");\r\n      equals(isNumber(new Date(2009, 1, 1)), false, "Date object");\r\n      equals(isNumber(new Object()), false, "Empty object");\r\n      equals(isNumber(function() {}), false, "Instance of a function");\r\n      equals(isNumber([]), false, "Empty Array");\r\n      equals(isNumber(["-10"]), false, "Array Negative integer string");\r\n      equals(isNumber(["0"]), false, "Array Zero string");\r\n      equals(isNumber(["5"]), false, "Array Positive integer string");\r\n      equals(isNumber([-16]), false, "Array Negative integer number");\r\n      equals(isNumber([0]), false, "Array Zero integer number");\r\n      equals(isNumber([32]), false, "Array Positive integer number");\r\n      equals(isNumber(["040"]), false, "Array Octal integer literal string");\r\n      equals(isNumber([0144]), false, "Array Octal integer literal");\r\n      equals(isNumber(["-040"]), false, "Array Negative Octal integer literal string");\r\n      equals(isNumber([-0144]), false, "Array Negative Octal integer literal");\r\n      equals(isNumber(["0xFF"]), false, "Array Hexadecimal integer literal string");\r\n      equals(isNumber([0xFFF]), false, "Array Hexadecimal integer literal");\r\n      equals(isNumber(["-0xFF"]), false, "Array Negative Hexadecimal integer literal string");\r\n      equals(isNumber([-0xFFF]), false, "Array Negative Hexadecimal integer literal");\r\n      equals(isNumber([1, 2]), false, "Array with more than 1 Positive interger number");\r\n      equals(isNumber([-1, -2]), false, "Array with more than 1 Negative interger number");\r\n    });\r\n  }\r\n\r\n  var functionsToTest = [\r\n\r\n    function(n) {\r\n      return !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\r\n    },\r\n\r\n    function(n) {\r\n      return !isNaN(n) &amp;&amp; !isNaN(parseFloat(n));\r\n    },\r\n\r\n    function(n) {\r\n      return !isNaN((n));\r\n    },\r\n\r\n    function(n) {\r\n      return !isNaN(parseFloat(n));\r\n    },\r\n\r\n    function(n) {\r\n      return typeof(n) != "boolean" &amp;&amp; !isNaN(n);\r\n    },\r\n\r\n    function(n) {\r\n      return parseFloat(n) === Number(n);\r\n    },\r\n\r\n    function(n) {\r\n      return parseInt(n) === Number(n);\r\n    },\r\n\r\n    function(n) {\r\n      return !isNaN(Number(String(n)));\r\n    },\r\n\r\n    function(n) {\r\n      return !isNaN(+(\'\' + n));\r\n    },\r\n\r\n    function(n) {\r\n      return (+n) == n;\r\n    },\r\n\r\n    function(n) {\r\n      return n &amp;&amp; /^-?\\d+(\\.\\d+)?$/.test(n + \'\');\r\n    },\r\n\r\n    function(n) {\r\n      return isFinite(Number(String(n)));\r\n    },\r\n\r\n    function(n) {\r\n      return isFinite(String(n));\r\n    },\r\n\r\n    function(n) {\r\n      return !isNaN(n) &amp;&amp; !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\r\n    },\r\n\r\n    function(n) {\r\n      return parseFloat(n) == n;\r\n    },\r\n\r\n    function(n) {\r\n      return (n - 0) == n &amp;&amp; n.length &gt; 0;\r\n    },\r\n\r\n    function(n) {\r\n      return typeof n === \'number\' &amp;&amp; isFinite(n);\r\n    },\r\n\r\n    function(n) {\r\n      return !Array.isArray(n) &amp;&amp; !isNaN(parseFloat(n)) &amp;&amp; isFinite(n.toString().replace(/^-/, \'\'));\r\n    }\r\n\r\n  ];\r\n\r\n\r\n  // Examines the functionsToTest array, extracts the return statement of each function\r\n  // and fills the toTest select element.\r\n  var fillToTestSelect = function() {\r\n    for (var i = 0; i &lt; functionsToTest.length; i++) {\r\n      var f = functionsToTest[i].toString();\r\n      var option = /[\\s\\S]*return ([\\s\\S]*);/.exec(f)[1];\r\n      $("#toTest").append(\'&lt;option value="\' + i + \'"&gt;\' + (i + 1) + \'. \' + option + \'&lt;/option&gt;\');\r\n    }\r\n  }\r\n\r\n  var performTest = function(functionNumber) {\r\n    reset(); // Reset previous test\r\n    $("#tests").html(""); //Clean test results\r\n    isNumber = functionsToTest[functionNumber]; // Override the isNumber global function with the one to test\r\n    testSuite(); // Run the test\r\n\r\n    // Get test results\r\n    var totalFail = 0;\r\n    var totalPass = 0;\r\n    $("b.fail").each(function() {\r\n      totalFail += Number($(this).html());\r\n    });\r\n    $("b.pass").each(function() {\r\n      totalPass += Number($(this).html());\r\n    });\r\n    $("#testresult").html(totalFail + " of " + (totalFail + totalPass) + " test failed.");\r\n\r\n    $("#banner").attr("class", "").addClass(totalFail &gt; 0 ? "fail" : "pass");\r\n  }\r\n\r\n  return {\r\n    performTest: performTest,\r\n    fillToTestSelect: fillToTestSelect,\r\n    testSuite: testSuite\r\n  };\r\n}();\r\n\r\n\r\n$(document).ready(function() {\r\n  testHelper.fillToTestSelect();\r\n  testHelper.performTest(0);\r\n\r\n  $("#toTest").change(function() {\r\n    testHelper.performTest($(this).children(":selected").val());\r\n  });\r\n});</code></pre>\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;\r\n&lt;script src="https://rawgit.com/Xotic750/testrunner-old/master/testrunner.js" type="text/javascript"&gt;&lt;/script&gt;\r\n&lt;link href="https://rawgit.com/Xotic750/testrunner-old/master/testrunner.css" rel="stylesheet" type="text/css"&gt;\r\n&lt;h1&gt;isNumber Test Cases&lt;/h1&gt;\r\n\r\n&lt;h2 id="banner" class="pass"&gt;&lt;/h2&gt;\r\n\r\n&lt;h2 id="userAgent"&gt;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11&lt;/h2&gt;\r\n\r\n&lt;div id="currentFunction"&gt;&lt;/div&gt;\r\n\r\n&lt;div id="selectFunction"&gt;\r\n  &lt;label for="toTest" style="font-weight:bold; font-size:Large;"&gt;Select function to test:&lt;/label&gt;\r\n  &lt;select id="toTest" name="toTest"&gt;\r\n  &lt;/select&gt;\r\n&lt;/div&gt;\r\n\r\n&lt;div id="testCode"&gt;&lt;/div&gt;\r\n\r\n&lt;ol id="tests"&gt;\r\n  &lt;li class="pass"&gt;\r\n    &lt;strong&gt;Integer Literals &lt;b style="color:black;"&gt;(0, 10, 10)&lt;/b&gt;&lt;/strong&gt;\r\n\r\n    &lt;ol style="display: none;"&gt;\r\n      &lt;li class="pass"&gt;Negative integer string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Zero string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Positive integer string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Negative integer number&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Zero integer number&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Positive integer number&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Octal integer literal string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Octal integer literal&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Hexadecimal integer literal string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Hexadecimal integer literal&lt;/li&gt;\r\n    &lt;/ol&gt;\r\n  &lt;/li&gt;\r\n\r\n  &lt;li class="pass"&gt;\r\n    &lt;strong&gt;Foating-Point Literals &lt;b style="color:black;"&gt;(0, 6, 6)&lt;/b&gt;&lt;/strong&gt;\r\n\r\n    &lt;ol style="display: none;"&gt;\r\n      &lt;li class="pass"&gt;Negative floating point string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Positive floating point string&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Negative floating point number&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Positive floating point number&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Exponential notation&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Exponential notation string&lt;/li&gt;\r\n    &lt;/ol&gt;\r\n  &lt;/li&gt;\r\n\r\n  &lt;li class="pass"&gt;\r\n    &lt;strong&gt;Non-Numeric values &lt;b style="color:black;"&gt;(0, 18, 18)&lt;/b&gt;&lt;/strong&gt;\r\n\r\n    &lt;ol style="display: none;"&gt;\r\n      &lt;li class="pass"&gt;Empty string: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Whitespace characters string: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Tab characters string: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Alphanumeric character string: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Non-numeric character string: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Boolean true literal: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Boolean false literal: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Number with preceding non-numeric characters: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Number with trailling non-numeric characters: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Undefined value: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Null value: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;NaN value: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Infinity primitive: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Positive Infinity: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Negative Infinity: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Date object: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Empty object: false&lt;/li&gt;\r\n\r\n      &lt;li class="pass"&gt;Instance of a function: false&lt;/li&gt;\r\n    &lt;/ol&gt;\r\n  &lt;/li&gt;\r\n&lt;/ol&gt;\r\n\r\n&lt;div id="main"&gt;\r\n  This page contains tests for a set of isNumber functions. To see them, take a look at the source.\r\n&lt;/div&gt;\r\n\r\n&lt;div&gt;\r\n  &lt;p class="result"&gt;Tests completed in 0 milliseconds.\r\n    &lt;br&gt;0 tests of 0 failed.&lt;/p&gt;\r\n&lt;/div&gt;</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 15103743, 'author': 'NaveenKumar1410', 'author_id': 2060915, 'author_profile': 'https://Stackoverflow.com/users/2060915', 'pm_score': 2, 'selected': False, 'text': "<p><strong>knockoutJs Inbuild library validation functions</strong> </p>\n\n<p>By extending it the field get validated</p>\n\n<p>1) number</p>\n\n<p><code>self.number = ko.observable(numberValue)</code><strong>.extend({ number: true})</strong>;</p>\n\n<p>TestCase</p>\n\n<pre><code>numberValue = '0.0'    --&gt; true\nnumberValue = '0'      --&gt; true\nnumberValue = '25'     --&gt; true\nnumberValue = '-1'     --&gt; true\nnumberValue = '-3.5'   --&gt; true\nnumberValue = '11.112' --&gt; true\nnumberValue = '0x89f'  --&gt; false\nnumberValue = ''       --&gt; false\nnumberValue = 'sfsd'   --&gt; false\nnumberValue = 'dg##$'  --&gt; false\n</code></pre>\n\n<p>2) digit</p>\n\n<p><code>self.number = ko.observable(numberValue)</code><strong>.extend({ digit: true})</strong>;</p>\n\n<p>TestCase</p>\n\n<pre><code>numberValue = '0'      --&gt; true\nnumberValue = '25'     --&gt; true\nnumberValue = '0.0'    --&gt; false\nnumberValue = '-1'     --&gt; false\nnumberValue = '-3.5'   --&gt; false\nnumberValue = '11.112' --&gt; false\nnumberValue = '0x89f'  --&gt; false\nnumberValue = ''       --&gt; false\nnumberValue = 'sfsd'   --&gt; false\nnumberValue = 'dg##$'  --&gt; false\n</code></pre>\n\n<p>3) min and max</p>\n\n<p><code>self.number = ko.observable(numberValue)</code><strong>.extend({ min: 5}).extend({ max: 10})</strong>;</p>\n\n<p>This field accept value between 5 and 10 only</p>\n\n<p>TestCase</p>\n\n<pre><code>numberValue = '5'    --&gt; true\nnumberValue = '6'    --&gt; true\nnumberValue = '6.5'  --&gt; true\nnumberValue = '9'    --&gt; true\nnumberValue = '11'   --&gt; false\nnumberValue = '0'    --&gt; false\nnumberValue = ''    --&gt; false\n</code></pre>\n"}, {'answer_id': 15997937, 'author': 'Phil', 'author_id': 1129712, 'author_profile': 'https://Stackoverflow.com/users/1129712', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1839844#1839844">@CMS\' answer</a>: Your snippet failed on whitespace cases on my machine using nodejs. So I combined it with \n<a href="https://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/174921#174921">@joel\'s answer</a> to the following:</p>\n\n<pre><code>is_float = function(v) {\n    return !isNaN(v) &amp;&amp; isFinite(v) &amp;&amp;\n        (typeof(v) == \'number\' || v.replace(/^\\s+|\\s+$/g, \'\').length &gt; 0);\n}\n</code></pre>\n\n<p>I unittested it with those cases that are floats:</p>\n\n<pre><code>var t = [\n        0,\n        1.2123,\n        \'0\',\n        \'2123.4\',\n        -1,\n        \'-1\',\n        -123.423,\n        \'-123.432\',\n        07,\n        0xad,\n        \'07\',\n        \'0xad\'\n    ];\n</code></pre>\n\n<p>and those cases that are no floats (including empty whitespaces and objects / arrays):</p>\n\n<pre><code>    var t = [\n        \'hallo\',\n        [],\n        {},\n        \'jklsd0\',\n        \'\',\n        "\\t",\n        "\\n",\n        \' \'\n    ];\n</code></pre>\n\n<p>Everything works as expected here. Maybe this helps.</p>\n\n<p>Full source code for this can be found <a href="https://github.com/philippkemmeter/philfw/blob/master/lib/ValueChecker.js" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 16654296, 'author': 'Arman', 'author_id': 1847185, 'author_profile': 'https://Stackoverflow.com/users/1847185', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a lil bit improved version (probably the fastest way out there) that I use instead of exact jQuery\'s variant, I really don\'t know why don\'t they use this one:</p>\n\n<pre><code>function isNumeric(val) {\n    return !isNaN(+val) &amp;&amp; isFinite(val);\n}\n</code></pre>\n\n<p>The downside of jQuery\'s version is that if you pass a string with leading numerics and trailing letters like <code>"123abc"</code> the <code>parseFloat | parseInt</code> will extract the numeric fraction out and return 123, BUT, the second guard <code>isFinite</code> will fail it anyway.\nWith the unary <code>+</code> operator it will die on the very first guard since + throws NaN for such hybrids :)\nA little performance yet I think a solid semantic gain.</p>\n'}, {'answer_id': 16973976, 'author': 'hobs', 'author_id': 623735, 'author_profile': 'https://Stackoverflow.com/users/623735', 'pm_score': 3, 'selected': False, 'text': '<p>Only problem I had with @CMS\'s <a href="https://stackoverflow.com/a/1830844/623735">answer</a> is the exclusion of <code>NaN</code> and Infinity, which are useful numbers for many situations. One way to check for <code>NaN</code>\'s is to check for numeric values that don\'t equal themselves, <code>NaN != NaN</code>!  So there are really 3 tests you\'d like to deal with ...</p>\n\n<pre><code>function isNumber(n) {\n  n = parseFloat(n);\n  return !isNaN(n) || n != n;\n}\nfunction isFiniteNumber(n) {\n  n = parseFloat(n);\n  return !isNaN(n) &amp;&amp; isFinite(n);\n}    \nfunction isComparableNumber(n) {\n  n = parseFloat(n);\n  return (n &gt;=0 || n &lt; 0);\n}\n\nisFiniteNumber(\'NaN\')\nfalse\nisFiniteNumber(\'OxFF\')\ntrue\nisNumber(\'NaN\')\ntrue\nisNumber(1/0-1/0)\ntrue\nisComparableNumber(\'NaN\')\nfalse\nisComparableNumber(\'Infinity\')\ntrue\n</code></pre>\n\n<p>My isComparableNumber is pretty close to another elegant <a href="https://stackoverflow.com/a/1561597/623735">answer</a>, but handles hex and other string representations of numbers.</p>\n'}, {'answer_id': 17559810, 'author': 'Mr Br', 'author_id': 2188869, 'author_profile': 'https://Stackoverflow.com/users/2188869', 'pm_score': 0, 'selected': False, 'text': '<p>I found simple solution, probably not best but it\'s working fine :)</p>\n\n<p>So, what I do is next, I parse string to Int and check if length size of new variable which is now int type is same as length of original string variable. Logically if size is the same it means string is fully parsed to int and that is only possible if string is "made" only of numbers.</p>\n\n<pre><code>var val=1+$(e).val()+\'\';\nvar n=parseInt(val)+\'\';\nif(val.length == n.length )alert(\'Is int\');\n</code></pre>\n\n<p>You can easily put that code in function and instead of alert use return true if int.\nRemember, if you use dot or comma in string you are checking it\'s still false cos you are parsing to int.</p>\n\n<p>Note: Adding 1+ on e.val so starting zero wouldn\'t be removed.</p>\n'}, {'answer_id': 19056758, 'author': 'Aaron Gong', 'author_id': 2215486, 'author_profile': 'https://Stackoverflow.com/users/2215486', 'pm_score': 2, 'selected': False, 'text': '<p>I have run the following below and it passes all the test cases...</p>\n\n<p>It makes use of the different way in which <code>parseFloat</code> and <code>Number</code> handle their inputs...</p>\n\n<pre><code>function IsNumeric(_in) {\n    return (parseFloat(_in) === Number(_in) &amp;&amp; Number(_in) !== NaN);\n}\n</code></pre>\n'}, {'answer_id': 20712631, 'author': 'daniel1426', 'author_id': 1985601, 'author_profile': 'https://Stackoverflow.com/users/1985601', 'pm_score': 1, 'selected': False, 'text': '<p>The following seems to works fine for many cases:</p>\n\n<pre><code>function isNumeric(num) {\n    return (num &gt; 0 || num === 0 || num === \'0\' || num &lt; 0) &amp;&amp; num !== true &amp;&amp; isFinite(num);\n}\n</code></pre>\n\n<p>This is built on top of this answer (which is for this answer too):\n<a href="https://stackoverflow.com/a/1561597/1985601">https://stackoverflow.com/a/1561597/1985601</a></p>\n'}, {'answer_id': 21096633, 'author': 'Sean the Bean', 'author_id': 814160, 'author_profile': 'https://Stackoverflow.com/users/814160', 'pm_score': 3, 'selected': False, 'text': '<p>I realize the original question did not mention jQuery, but if you do use jQuery, you can do:</p>\n\n<pre><code>$.isNumeric(val)\n</code></pre>\n\n<p>Simple.</p>\n\n<p><a href="https://api.jquery.com/jQuery.isNumeric/" rel="noreferrer">https://api.jquery.com/jQuery.isNumeric/</a> (as of jQuery 1.7)</p>\n'}, {'answer_id': 23049711, 'author': 'donquixote', 'author_id': 246724, 'author_profile': 'https://Stackoverflow.com/users/246724', 'pm_score': 2, 'selected': False, 'text': '<p>I realize this has been answered many times, but the following is a decent candidate which can be useful in some scenarios.</p>\n\n<p>it should be noted that it assumes that \'.42\' is NOT a number, and \'4.\' is NOT a number, so this should be taken into account.</p>\n\n<pre><code>function isDecimal(x) {\n  return \'\' + x === \'\' + +x;\n}\n\nfunction isInteger(x) {\n  return \'\' + x === \'\' + parseInt(x);\n}\n</code></pre>\n\n<p>The <code>isDecimal</code> passes the following test:</p>\n\n<pre><code>function testIsNumber(f) {\n  return f(\'-1\') &amp;&amp; f(\'-1.5\') &amp;&amp; f(\'0\') &amp;&amp; f(\'0.42\')\n    &amp;&amp; !f(\'.42\') &amp;&amp; !f(\'99,999\') &amp;&amp; !f(\'0x89f\')\n    &amp;&amp; !f(\'#abcdef\') &amp;&amp; !f(\'1.2.3\') &amp;&amp; !f(\'\') &amp;&amp; !f(\'blah\');\n}\n</code></pre>\n\n<p>The idea here is that every number or integer has one "canonical" string representation, and every non-canonical representation should be rejected. So we cast to a number and back, and see if the result is the original string.</p>\n\n<p>Whether these functions are useful for you depends on the use case. One feature is that <em>distinct strings represent distinct numbers</em> (if both pass the <code>isNumber()</code> test).</p>\n\n<p>This is relevant e.g. for numbers as object property names.</p>\n\n<pre><code>var obj = {};\nobj[\'4\'] = \'canonical 4\';\nobj[\'04\'] = \'alias of 4\';\nobj[4];  // prints \'canonical 4\' to the console.\n</code></pre>\n'}, {'answer_id': 25861284, 'author': 'Nik', 'author_id': 1180387, 'author_profile': 'https://Stackoverflow.com/users/1180387', 'pm_score': -1, 'selected': False, 'text': '<p>I use this way to chack that varible is numeric:</p>\n\n<pre><code>v * 1 == v\n</code></pre>\n'}, {'answer_id': 27471814, 'author': 'Simon Hi', 'author_id': 2458202, 'author_profile': 'https://Stackoverflow.com/users/2458202', 'pm_score': 1, 'selected': False, 'text': "<pre><code>function isNumber(n) {\n    return (n===n+''||n===n-0) &amp;&amp; n*0==0 &amp;&amp; /\\S/.test(n);\n}\n</code></pre>\n\n<p><strong>Explanations:</strong></p>\n\n<p><code>(n===n-0||n===n+'')</code> verifies if n is a number or a string <em>(discards arrays, boolean, date, null, ...)</em>. You can replace <code>(n===n-0||n===n+'')</code> by <code>n!==undefined &amp;&amp; n!==null &amp;&amp; (n.constructor===Number||n.constructor===String)</code>: significantly faster but less concise.</p>\n\n<p><code>n*0==0</code> verifies if n is a finite number as <code>isFinite(n)</code> does. If you need to check strings that represent negative hexadecimal, just replace <code>n*0==0</code> by something like <code>n.toString().replace(/^\\s*-/,'')*0==0</code>.<br>\nIt costs a little of course, so if you don't need it, don't use it.</p>\n\n<p><code>/\\S/.test(n)</code> discards empty strings or strings, that contain only white-spaces <em>(necessary since <code>isFinite(n) or n*0==0</code> return a <code>false</code> positive in this case)</em>. You can reduce the number of call to <code>.test(n)</code> by using <code>(n!=0||/0/.test(n))</code> instead of <code>/\\S/.test(n)</code>, or you can use a slightly faster but less concise test such as <code>(n!=0||(n+'').indexOf('0')&gt;=0)</code>: tiny improvement.</p>\n"}, {'answer_id': 27622495, 'author': 'John', 'author_id': 606371, 'author_profile': 'https://Stackoverflow.com/users/606371', 'pm_score': 2, 'selected': False, 'text': "<p>None of the answers return <code>false</code> for empty strings, a fix for that...</p>\n\n<pre><code>function is_numeric(n)\n{\n return (n != '' &amp;&amp; !isNaN(parseFloat(n)) &amp;&amp; isFinite(n));\n}\n</code></pre>\n"}, {'answer_id': 34791974, 'author': 'Dmitry Sheiko', 'author_id': 998008, 'author_profile': 'https://Stackoverflow.com/users/998008', 'pm_score': 1, 'selected': False, 'text': '<p>One can use a type-check library like <a href="https://github.com/arasatasaygin/is.js" rel="nofollow">https://github.com/arasatasaygin/is.js</a> or just extract a check snippet from there (<a href="https://github.com/arasatasaygin/is.js/blob/master/is.js#L131" rel="nofollow">https://github.com/arasatasaygin/is.js/blob/master/is.js#L131</a>):</p>\n\n<pre><code>is.nan = function(value) {    // NaN is number :) \n  return value !== value;\n};\n // is a given value number?\nis.number = function(value) {\n    return !is.nan(value) &amp;&amp; Object.prototype.toString.call(value) === \'[object Number]\';\n};\n</code></pre>\n\n<p>In general if  you need it to validate parameter types (on entry point of function call), you can go with JSDOC-compliant contracts (<a href="https://www.npmjs.com/package/bycontract" rel="nofollow">https://www.npmjs.com/package/bycontract</a>):</p>\n\n<pre><code>/**\n * This is JSDOC syntax\n * @param {number|string} sum\n * @param {Object.&lt;string, string&gt;} payload\n * @param {function} cb\n */\nfunction foo( sum, payload, cb ) {\n  // Test if the contract is respected at entry point\n  byContract( arguments, [ "number|string", "Object.&lt;string, string&gt;", "function" ] );\n}\n// Test it\nfoo( 100, { foo: "foo" }, function(){}); // ok\nfoo( 100, { foo: 100 }, function(){}); // exception\n</code></pre>\n'}, {'answer_id': 35324436, 'author': 'studio-klik', 'author_id': 4813369, 'author_profile': 'https://Stackoverflow.com/users/4813369', 'pm_score': 2, 'selected': False, 'text': '<p>If you need to validate a special set of decimals y\nyou can use this simple javascript:</p>\n\n<p><a href="http://codesheet.org/codesheet/x1kI7hAD" rel="nofollow">http://codesheet.org/codesheet/x1kI7hAD</a></p>\n\n<pre><code>&lt;input type="text" name="date" value="" pattern="[0-9]){1,2}(\\.){1}([0-9]){2}" maxlength="6" placeholder="od npr.: 16.06" onchange="date(this);" /&gt;\n</code></pre>\n\n<p>The Javascript:</p>\n\n<pre><code>function date(inputField) {        \n  var isValid = /^([0-9]){1,2}(\\.){1}([0-9]){2}$/.test(inputField.value);   \n  if (isValid) {\n    inputField.style.backgroundColor = \'#bfa\';\n  } else {\n    inputField.style.backgroundColor = \'#fba\';\n  }\n  return isValid;\n}\n</code></pre>\n'}, {'answer_id': 36318751, 'author': 'Shishir Arora', 'author_id': 3221274, 'author_profile': 'https://Stackoverflow.com/users/3221274', 'pm_score': 2, 'selected': False, 'text': '<p><code>isNumeric=(el)=&gt;{return Boolean(parseFloat(el)) &amp;&amp; isFinite(el)}</code></p>\n\n<p>Nothing very different but we can use Boolean constructor</p>\n'}, {'answer_id': 36533370, 'author': 'adius', 'author_id': 1850340, 'author_profile': 'https://Stackoverflow.com/users/1850340', 'pm_score': 3, 'selected': False, 'text': '<p>To check if a variable contains a valid number and not\njust a String which looks like a number,\n<code>Number.isFinite(value)</code> can be used.</p>\n\n<p>This is part of the language since\n<a href="http://www.ecma-international.org/ecma-262/6.0/#sec-number.isfinite" rel="noreferrer">ES2015</a></p>\n\n<p>Examples:</p>\n\n<pre><code>Number.isFinite(Infinity)   // false\nNumber.isFinite(NaN)        // false\nNumber.isFinite(-Infinity)  // false\n\nNumber.isFinite(0)          // true\nNumber.isFinite(2e64)       // true\n\nNumber.isFinite(\'0\')        // false\nNumber.isFinite(null)       // false\n</code></pre>\n'}, {'answer_id': 37331792, 'author': 'Syed Nasir Abbas', 'author_id': 585237, 'author_profile': 'https://Stackoverflow.com/users/585237', 'pm_score': -1, 'selected': False, 'text': '<pre><code>function isNumeric(n) {\n    var isNumber = true;\n\n    $.each(n.replace(/ /g,\'\').toString(), function(i, v){\n        if(v!=\',\' &amp;&amp; v!=\'.\' &amp;&amp; v!=\'-\'){\n            if(isNaN(v)){\n               isNumber = false;\n               return false;\n            }\n         }\n     });\n\n    return isNumber;\n}\n\nisNumeric(-3,4567.89);   // true &lt;br&gt;\n\nisNumeric(3,4567.89);   // true &lt;br&gt;\n\nisNumeric("-3,4567.89");   // true &lt;br&gt;\n\nisNumeric(3d,4567.89);   // false\n</code></pre>\n'}, {'answer_id': 37384296, 'author': 'paulalexandru', 'author_id': 3522687, 'author_profile': 'https://Stackoverflow.com/users/3522687', 'pm_score': 1, 'selected': False, 'text': '<p>Best way to do this is like this:</p>\n\n<pre><code>function isThisActuallyANumber(data){\n    return ( typeof data === "number" &amp;&amp; !isNaN(data) );\n}\n</code></pre>\n'}, {'answer_id': 37975166, 'author': 'John Mikic', 'author_id': 1636207, 'author_profile': 'https://Stackoverflow.com/users/1636207', 'pm_score': 3, 'selected': False, 'text': '<p>I think parseFloat function can do all the work here. The function below passes all the tests on this page including <code>isNumeric(Infinity) == true</code>:</p>\n\n<pre><code>function isNumeric(n) {\n\n    return parseFloat(n) == n;\n}\n</code></pre>\n'}, {'answer_id': 38882756, 'author': 'chrmcpn', 'author_id': 3626940, 'author_profile': 'https://Stackoverflow.com/users/3626940', 'pm_score': 2, 'selected': False, 'text': "<pre><code>function inNumeric(n){\n   return Number(n).toString() === n;\n}\n</code></pre>\n\n<p>If n is numeric <code>Number(n)</code> will return the numeric value and <code>toString()</code> will turn it back to a string. But if n isn't numeric <code>Number(n)</code> will return <code>NaN</code> so it won't match the original <code>n</code></p>\n"}, {'answer_id': 41526723, 'author': 'solimanware', 'author_id': 4591364, 'author_profile': 'https://Stackoverflow.com/users/4591364', 'pm_score': 2, 'selected': False, 'text': '<p>I think my code is perfect ...</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>/**\r\n * @param {string} s\r\n * @return {boolean}\r\n */\r\nvar isNumber = function(s) {\r\n    return s.trim()!=="" &amp;&amp; !isNaN(Number(s));\r\n};</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 42018658, 'author': 'Saurabh Chandra Patel', 'author_id': 1371778, 'author_profile': 'https://Stackoverflow.com/users/1371778', 'pm_score': -1, 'selected': False, 'text': '<pre><code>$(\'.rsval\').bind(\'keypress\', function(e){  \n        var asciiCodeOfNumbers = [48,46, 49, 50, 51, 52, 53, 54, 54, 55, 56, 57];\n        var keynum = (!window.event) ? e.which : e.keyCode; \n        var splitn = this.value.split("."); \n        var decimal = splitn.length;\n        var precision = splitn[1]; \n        if(decimal == 2 &amp;&amp; precision.length &gt;= 2  ) { console.log(precision , \'e\');   e.preventDefault(); } \n        if( keynum == 46 ){  \n            if(decimal &gt; 2) { e.preventDefault(); }  \n        } \n        if ($.inArray(keynum, asciiCodeOfNumbers) == -1)\n            e.preventDefault();    \n  });\n</code></pre>\n'}, {'answer_id': 42419193, 'author': 'Vixed', 'author_id': 1076753, 'author_profile': 'https://Stackoverflow.com/users/1076753', 'pm_score': 2, 'selected': False, 'text': "<p>You can minimize this function in a lot of way, and you can also implement it with a custom regex for negative values or custom charts:</p>\n\n<pre><code>$('.number').on('input',function(){\n    var n=$(this).val().replace(/ /g,'').replace(/\\D/g,'');\n    if (!$.isNumeric(n))\n        $(this).val(n.slice(0, -1))\n    else\n        $(this).val(n)\n});\n</code></pre>\n"}, {'answer_id': 47009183, 'author': 'Alston', 'author_id': 1599462, 'author_profile': 'https://Stackoverflow.com/users/1599462', 'pm_score': 2, 'selected': False, 'text': "<p>No need to use extra lib.</p>\n\n<pre><code>const IsNumeric = (...numbers) =&gt; {\n  return numbers.reduce((pre, cur) =&gt; pre &amp;&amp; !!(cur === 0 || +cur), true);\n};\n</code></pre>\n\n<p>Test</p>\n\n<pre><code>&gt; IsNumeric(1)\ntrue\n&gt; IsNumeric(1,2,3)\ntrue\n&gt; IsNumeric(1,2,3,0)\ntrue\n&gt; IsNumeric(1,2,3,0,'')\nfalse\n&gt; IsNumeric(1,2,3,0,'2')\ntrue\n&gt; IsNumeric(1,2,3,0,'200')\ntrue\n&gt; IsNumeric(1,2,3,0,'-200')\ntrue\n&gt; IsNumeric(1,2,3,0,'-200','.32')\ntrue\n</code></pre>\n"}, {'answer_id': 54382602, 'author': 'Mhmdrz_A', 'author_id': 5953610, 'author_profile': 'https://Stackoverflow.com/users/5953610', 'pm_score': 2, 'selected': False, 'text': '<p>A simple and clean solution by leveraging language\'s dynamic type checking:</p>\n\n<pre class="lang-js prettyprint-override"><code>function IsNumeric (string) {\n   if(string === \' \'.repeat(string.length)){\n     return false\n   }\n   return string - 0 === string * 1\n}\n\n</code></pre>\n\n<p>if you don\'t care about white-spaces you can remove that " if " </p>\n\n<p>see test cases below</p>\n\n<p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">\r\n<div class="snippet-code snippet-currently-hidden">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>function IsNumeric (string) {\r\n   if(string === \' \'.repeat(string.length)){\r\n      return false\r\n   }\r\n   return string - 0 === string * 1\r\n}\r\n\r\n\r\nconsole.log(\'-1\' + \' → \' + IsNumeric(\'-1\'))    \r\nconsole.log(\'-1.5\' + \' → \' + IsNumeric(\'-1.5\')) \r\nconsole.log(\'0\' + \' → \' + IsNumeric(\'0\'))     \r\nconsole.log(\'0.42\' + \' → \' + IsNumeric(\'0.42\'))   \r\nconsole.log(\'.42\' + \' → \' + IsNumeric(\'.42\'))    \r\nconsole.log(\'99,999\' + \' → \' + IsNumeric(\'99,999\'))\r\nconsole.log(\'0x89f\' + \' → \' + IsNumeric(\'0x89f\'))  \r\nconsole.log(\'#abcdef\' + \' → \' + IsNumeric(\'#abcdef\'))\r\nconsole.log(\'1.2.3\' + \' → \' + IsNumeric(\'1.2.3\')) \r\nconsole.log(\'\' + \' → \' + IsNumeric(\'\'))    \r\nconsole.log(\'33 \' + \' → \' + IsNumeric(\'33 \'))</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 58091055, 'author': 'MarredCheese', 'author_id': 5405967, 'author_profile': 'https://Stackoverflow.com/users/5405967', 'pm_score': 2, 'selected': False, 'text': "<h1>Here's a <em>dead-simple</em> one (tested in Chrome, Firefox, and IE):</h1>\n\n<pre><code>function isNumeric(x) {\n  return parseFloat(x) == x;\n}\n</code></pre>\n\n<p>Test cases from question:</p>\n\n<pre><code>console.log('trues');\nconsole.log(isNumeric('-1'));\nconsole.log(isNumeric('-1.5'));\nconsole.log(isNumeric('0'));\nconsole.log(isNumeric('0.42'));\nconsole.log(isNumeric('.42'));\n\nconsole.log('falses');\nconsole.log(isNumeric('99,999'));\nconsole.log(isNumeric('0x89f'));\nconsole.log(isNumeric('#abcdef'));\nconsole.log(isNumeric('1.2.3'));\nconsole.log(isNumeric(''));\nconsole.log(isNumeric('blah'));\n</code></pre>\n\n<p>Some more test cases:</p>\n\n<pre><code>console.log('trues');\nconsole.log(isNumeric(0));\nconsole.log(isNumeric(-1));\nconsole.log(isNumeric(-500));\nconsole.log(isNumeric(15000));\nconsole.log(isNumeric(0.35));\nconsole.log(isNumeric(-10.35));\nconsole.log(isNumeric(2.534e25));\nconsole.log(isNumeric('2.534e25'));\nconsole.log(isNumeric('52334'));\nconsole.log(isNumeric('-234'));\nconsole.log(isNumeric(Infinity));\nconsole.log(isNumeric(-Infinity));\nconsole.log(isNumeric('Infinity'));\nconsole.log(isNumeric('-Infinity'));\n\nconsole.log('falses');\nconsole.log(isNumeric(NaN));\nconsole.log(isNumeric({}));\nconsole.log(isNumeric([]));\nconsole.log(isNumeric(''));\nconsole.log(isNumeric('one'));\nconsole.log(isNumeric(true));\nconsole.log(isNumeric(false));\nconsole.log(isNumeric());\nconsole.log(isNumeric(undefined));\nconsole.log(isNumeric(null));\nconsole.log(isNumeric('-234aa'));\n</code></pre>\n\n<p>Note that it considers infinity a number.</p>\n"}, {'answer_id': 71097252, 'author': 'chickens', 'author_id': 1602301, 'author_profile': 'https://Stackoverflow.com/users/1602301', 'pm_score': 0, 'selected': False, 'text': "<p>With regex we can cover all the cases ask in the question. Here it is:</p>\n<p><strong>isNumeric for all integers and decimals:</strong></p>\n<pre><code>const isNumeric = num =&gt; /^-?[0-9]+(?:\\.[0-9]+)?$/.test(num+'');\n</code></pre>\n<p><strong>isInteger for just integers:</strong></p>\n<pre><code>const isInteger = num =&gt; /^-?[0-9]+$/.test(num+'');\n</code></pre>\n"}, {'answer_id': 72830995, 'author': 'Mykola Uspalenko', 'author_id': 8413306, 'author_profile': 'https://Stackoverflow.com/users/8413306', 'pm_score': 0, 'selected': False, 'text': '<p>Need to check for the null/undefined condition and remove commas (for the US number format) if <code>typeof n === \'string\'</code>.</p>\n<pre><code>function isNumeric(n)\n{\n    if(n === null || typeof n === \'undefined\')\n         return false;\n\n    if(typeof n === \'string\')\n        n = n.split(\',\').join(\'\');\n\n    return !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\n}\n</code></pre>\n<p><a href="https://jsfiddle.net/NickU/nyzeot03/3/" rel="nofollow noreferrer">https://jsfiddle.net/NickU/nyzeot03/3/</a></p>\n'}, {'answer_id': 74242649, 'author': 'Timothy C. Quinn', 'author_id': 286807, 'author_profile': 'https://Stackoverflow.com/users/286807', 'pm_score': 0, 'selected': False, 'text': "<p>If you preference is to have your numeric function predictates to be implicitly strict (eg, no parsing of strings), then this should do the trick.</p>\n<pre><code>function isNumeric(n, parse) {\n    var t = typeof(n);\n    if (parse){\n        if (t !== 'number' &amp;&amp; t !=='string') return false;\n        return !isNaN(parseFloat(n)) &amp;&amp; isFinite(n);\n    }else{\n        if (t !== 'number') return false;\n        return !isNaN(n) &amp;&amp; isFinite(n) &amp;&amp; !_.isString(n);\n    }\n}\n\nfunction isInteger(n, parse) {    \n    return isNumeric(n, parse) &amp;&amp; n % 1 === 0;\n}\n\nfunction isFloat(n, parse) {\n    return isNumeric(n, parse) &amp;&amp; n % 1 !== 0;\n}\n</code></pre>\n<p>If you want the code to parse strings, then just pass the true in the parse parameter.</p>\n<p>This is modification of <code>underscore-contrib</code>'s approach which is to be implicitly loose and try parsing strings and even returns true for <code>isNumeric([1])</code>, which can be a real trap for people. My approach above will also be faster as it only calls <code>parseFloat()</code> when <code>parse</code> = true.</p>\n"}]
['https://Stackoverflow.com/questions/18082', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/29/']