views:

8918

answers:

9

Hi Guys, I have a textbox for 'Area '.I need a regularexpression to validate textbox such that it should allow decimals to enter but no characters.Anyone can help me

A: 

'^[0-9]*$'

kender
Don't forget - he also wants decimal '.' character
Marc Novakowski
That can also be a comma "," depending on what country you live in.
some
A: 

If you want decimals, this should work:

/^(?:\d+\.?)|(?:\d*\.\d+)$/
Ben Alpert
This will not match numbers in the following notation: 1,000.00
Unkwntech
+1  A: 

What is the format of your decimal numbers you will support in your field ?

This "Simple Regular expression for decimal numbers?" StackOverflow question details the possible regex.

^\d+(\.\d{1,2})?$

Can ensure a number with one or two decimals, but would not work with 34. (dot without decimal)

You have lots of possible regex listed here.

Unkwntech is a good complete regexp but would allow 1,15223,11,00.

I would rather use:

[-+]?(?:\d(\,?(?>\d{3}[.,]))?)*(?:\.\d*)?

Meaning, if you are using a ,, do so only if it is followed with 3 digits (and then another , or a dot for decimal values. That enforces digital grouping, even though, as pointed out by Paul in the comments, there are locale with more than three digits after a comma...

VonC
Correct: that is why I asked about the format.
VonC
Not all locales have 3 digits after a comma ...
Paul
True, that is why I asked about the format (bis ;) )
VonC
+1  A: 

This regex can validate integers and optional floating point numbers:

[0-9]*.?[0-9]+

And instead of using regular expressions, you could use isNaN(value) to check if the numerical value is not a number (NaN).

CMS
+1 for suggesting *actual* number parsing (using a library function).
strager
A: 

For decimal values you would have to allow [.] as well so this should work for you :

^[-+]?\d*.?\d*$

(this would also accept + and - symbols)

You can get more specific expression from the RegExLib here :

Jomit

Jomit
+1 for the + and -
some
However, the dot matches all characters, like A, B, comma, etc... you want to escape the dot like this: ^[-+]?\d*\.?\d*$
some
This will not match numbers in the following notation: 1,000.00
Unkwntech
Unkwnteck: And not 1,0 that is what my country uses, or 1E10. But if that matters to the original poster or not I don't know.
some
+1  A: 

How about

[-+]?(?:\d\,?){0,}(?:\.\d*)?

It will match all the following exapmles that have a *:

0*
0.0*
222.0*
1,000.00*
100,000.00*
-100,000,000,000.12*
asdf
blah.blah

Unkwntech
Thanks to Jomit for the note on +|-
Unkwntech
You can use ? instead of {0,1} to speed up parsing and make the regexp easier to read.
strager
Good one, I always seem to forget about ?
Unkwntech
-1 for it would allow 1,04400.00 (more than three digits after a ','). See my completed answer for a more robust regex
VonC
I think it's better to just strip the , anyway as they likely will be irrelivent to the actual use anyway.
Unkwntech
It also allows this: 122,2,0,0,2,
some
+4  A: 

Why not try something that's already built-in to JavaScript: the parseFloat function?

Zach Hirsch
because parseFloat allows all kinds of invalid input that you don't want to see.
eplawless
Do you mean that parseFloat("123 foo") == 123 ? Then couldn't you just reject the string if there's whitespace in it?
Zach Hirsch
@Hirsch, Then parseFloat("123foo") would workfine, I think. Still, +1, for stealing my answer. ;P
strager
A: 

You can also use the "isFinite" function:

http://www.w3schools.com/jsref/jsref_isFinite.asp

I.devries
A: 

this expression takes single space also at the start... which is not valid