views:

113

answers:

4

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?

+2  A: 
var valid = (value.match(/^-?\d*(\.\d+)?$/));
mkedobbs
What about negative numbers?
FrustratedWithFormsDesigner
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
A: 
var valid = (value.match(/^[\d.]+$/));
S.Mark
+8  A: 

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
It's also funny because the accepted answer doesn't accept negative numbers :)
llamaoo7
Nor does the accepted answer accept numbers such as `.42` or `37.`.
Tenner
Nice but it will need some extra work for white space characters and empty string: `!isNaN(' ') == true;` `!isNaN('\t\t') == true;` `!isNaN('') == true;`
CMS
@CMS: Damn it, you're right! Edited in shame :(
Crescent Fresh
curse you JavaScript! "[tab][space][tab]" is **Not a Number** in my book! :-P
scunliffe
+3  A: 

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);
}
CMS
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
@Crescent: LOL, actually it fails 10 of my tests hehe, I'll add it there in a minute...
CMS
@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
@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
CMS