There is another question, which deals with a mandatory decimal place, in this case, it is optional, and only if there are decimals. I need to check to see if a number is valid in a numeric input by comparing it to my regex on paste and keyed in. The number can contain up to 9 numbers, and if it has a decimal point, up to 6 decimal places.
For example:
123456789.123456
is a valid number, but
1234567890.1234567
or
1234567890.
is not valid. My regex thus far is:
/^(\d{0,9})(\.{0,1}\d{0,6})*$/
..but it still allows a decimal place without decimals.
Use
^\d{0,9}(?:\.\d{1,6})?$
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\d{0,9} digits (0-9) (between 0 and 9 times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d{1,6} digits (0-9) (between 1 and 6 times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string