Decimal number regular expression, where digit after decimal is optional

Use the following:

/^\d*\.?\d*$/
  • ^ – Beginning of the line;
  • \d* – 0 or more digits;
  • \.? – An optional dot (escaped, because in regex, . is a special character);
  • \d* – 0 or more digits (the decimal part);
  • $ – End of the line.

This allows for .5 decimal rather than requiring the leading zero, such as 0.5

Leave a Comment