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
views:
8918answers:
9If you want decimals, this should work:
/^(?:\d+\.?)|(?:\d*\.\d+)$/
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...
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).
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
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
Why not try something that's already built-in to JavaScript: the parseFloat function?