Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
1.123456
1.12
1.236

these are valid

1.1234567
5.123456789
these are not valid

I tried ^\d{0,9}(\.\d{0,9})?$ but it didnt work
Posted
Updated 11-Sep-15 5:25am
v2
Comments
Sergey Alexandrovich Kryukov 11-Sep-15 11:44am    
Because your regular expression does not even contain the figure 6... :-)
Also, why doing so? Validation of floating-point data is much more complex.
—SA
Patrice T 11-Sep-15 18:47pm    
What is the usage ?

Try changing the 9 to a 6:
^\d{0,9}(\.\d{0,6})?$
 
Share this answer
 
Please see my comment to the question. Is looks like validation of some floating-point input. Who cares how many digits are there. You only need to take care of valid format, and, on top of it, probably a valid range. But how?

Simple. Use parseFloat:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat[^].

Failure will be indicated by null value returned. If parsing is successful, you get floating-point numeric object which you can additionally check for the expect range of values. That's all.

Alternatively or additionally, you can use masked text box for input. Please see, for example: http://jsfiddle.net/SO_AMK/SEXAj[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900