27

Currently i'm using this reg exp:

var valid = (value.match(/^\d+$/));

But for digits like '0.40', or '2.43' this doesn't work. How can I change that reg exp above to match floats as well?

Ali
  • 261,656
  • 265
  • 575
  • 769

10 Answers10

63

You don't need regex for this! isNaN will cast thine value to Number:

var valid = !isNaN(value);

Eg:

!isNaN('0'); // true
!isNaN('34.56'); // true
!isNaN('.34'); // true
!isNaN('-34'); // true
!isNaN('foo'); // false
!isNaN('08'); // true

Reluctant Edit (thanks CMS):

Blasted type coercion, !isNaN(''), !isNaN(' '), !isNaN('\n\t'), etc are all true!

Whitespace test + isNaN FTW:

var valid = !/^\s*$/.test(value) && !isNaN(value);

Yuck.

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
37
var valid = (value.match(/^-?\d*(\.\d+)?$/));
mkedobbs
  • 4,327
  • 2
  • 23
  • 28
  • My bad. The corrected entry should address that and the situation where there are no numbers before the decimal point. I'm assuming that there is a reason there is a preference to regex for this question (rather than using isNaN) such as tying it to some validation framework. – mkedobbs Dec 02 '09 at 04:18
  • 1
    This also matches '' (empty string) and '-' (single dash). – stephband Apr 07 '14 at 18:38
  • 2
    this wrong regex mark non-valid for example these valid numbers: +42 42. 42e42 – sarkiroka Aug 07 '14 at 20:19
28

Continuing with the @Crescent Fresh approach, some time ago, I had to do number validation, but I needed to verify if a variable contained a number without knowing its type, it could be a String containing a numeric value as in this case, (I had to consider also exponential notation, etc.), a Number object, basically anything I couldn't make any type assumption.

And I had to take care about implicit type conversion, for example as I pointed to @Crescent, isNaN wasn't enough for my case:

// string values
!isNaN(' ') == true;
!isNaN('\t\t') == true;
!isNaN('') == true;

// boolean values
!isNaN(true) == true;
!isNaN(false) == true;

// etc..

I ended up writing a set of 30+ unit tests that you can find and run here, and the following function, is the one that passes all my tests:

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}
Community
  • 1
  • 1
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • FTW! Just found this: http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric You need to add this answer there. The accepted answer fails outright on whitespace strings. – Crescent Fresh Dec 02 '09 at 04:34
  • @Crescent: LOL, actually it fails 10 of my tests hehe, I'll add it there in a minute... – Christian C. Salvadó Dec 02 '09 at 04:40
  • @CMS: it's funny, Joel just updated his answer recently to account for it giving false positives on `''` (empty string). Really really nice test suite btw. – Crescent Fresh Dec 02 '09 at 04:42
  • @Crescent: Thanks, the QUnit testrunner is really flexible. Added my answer, but I think it may remain buried on bottom.... http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844 – Christian C. Salvadó Dec 02 '09 at 05:50
  • Answers like this make me wish I could give more than +1. Anything backed up with that many tests deserves at least 2 :) – Dan M Mar 27 '13 at 19:28
  • `jQuery.isNumeric` also passes all your tests. – ivan.a.bovin Jun 02 '14 at 16:04
  • This matches "00.0" and does not allow spaces between the sign (+-) and the value as in "+ 1". – cquezel May 10 '20 at 12:14
8

Here's my RegExp version.

var n = /^[+-]?((\.\d+)|(\d+(\.\d+)?))$/;

Match

n.test('3');
n.test('3.0');
n.test('0.3');
n.test('.3');
n.test('+3');
n.test('+3.0');
n.test('+0.3');
n.test('+.3');
n.test('-3');
n.test('-3.0');
n.test('-0.3');
n.test('-.3');

Don't match

n.test('');
n.test('+');
n.test('-');
KLicheR
  • 2,034
  • 1
  • 13
  • 4
  • This is also not matching valid floats with exponential scientific notation like `1E-6` (0.000001) or `1E6` (1000000). – Wilt May 10 '16 at 15:01
  • 3
    This doesn't capture a trailing decimal. eg. `3.`. To capture that I have: `/^[+-]?((\.\d+)|(\d+(\.\d+)?)|(\d+\.))$/` – nemo May 08 '18 at 22:52
1

You can try my solution: /^-?\d+(\.?\d+)?$/.test(text)

0
var valid = (value.match(/^[\d.]+$/));
YOU
  • 120,166
  • 34
  • 186
  • 219
0
var valid = value.match(/\d+?\.\d+/);
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
0

This is what I use

/[\+]?([\-]?([0-9]{1,})?[\.]?[0-9]{1,})/
Eric
  • 9,870
  • 14
  • 66
  • 102
  • This matches "00.0" which is not a valid floating point. The regEx does not consume the trailling decimal as in "1.". It does not allow spaces between the sign (+-) and the value as in "+ 1". – cquezel May 10 '20 at 11:49
0

Here is validation without any regular expressions, using the "validity" property of the element itself:

var valid = input_element.validity.valid;
  • 1
    -1? I thought it's obvious, especially there is many lines of code above which also does not require any comments. Topic itself describes what this code are doing. It validate the correctness of the input for some element. Without any regular expressions. – Aleksey Kuznetsov Mar 21 '19 at 20:32
0

Here's a regex that accepts and captures all & only valid non-negative floats (but it doesn't handle exponents):

((?:\d+\.\d*)|(?:\d*\.?\d+))

Accepts 1, 1.0, 1., 0.1, .1 but not "." nor anything with more than one "."

See sample at regex101: https://regex101.com/r/t7eikU/1

GaryO
  • 5,873
  • 1
  • 36
  • 61