/^/
EasyNumbers3 min read

Decimal Number Regex

Validates a fixed-point decimal number that requires digits on both sides of the decimal point, with an optional leading minus sign.

#decimal#numbers#validation#fixed-point#forms

Regex Pattern

^-?\d+\.\d+$

Pattern Breakdown

Hover over a token to see what it does.

^-?\d+\.\d+$
TokenMeaning
^Anchors the match to the start of the string.
-?An optional leading minus sign for negative values.
\d+One or more digits for the integer part, required before the decimal point.
\.A literal decimal point.
\d+One or more digits for the fractional part, required after the decimal point.
$Anchors the match to the end of the string.

Detailed Explanation

What it does

This pattern matches a fixed-point decimal number such as 3.14 or -0.5, requiring at least one digit before and after a single decimal point. Plain integers without a decimal point, and shorthand forms like .5 or 3., are intentionally rejected since this pattern targets strictly two-sided decimal notation.

Why it works

Both `\d+` groups are mandatory, so the decimal point must have real digits on either side, and the literal `\.` (escaped so it isn't treated as 'any character') sits exactly once between them. Anchoring with `^` and `$` prevents a value like 3.14.15 from partially matching just the 3.14 portion.

Common use cases

  • Validating price, weight, or measurement fields that must show explicit decimal precision
  • Enforcing a strict two-sided decimal format distinct from shorthand notations like .5
  • Pre-checking numeric CSV columns that are expected to always include a fractional part
  • Filtering configuration values that must be expressed as fixed-point decimals

Edge cases

  • 3. and .5 both fail because this pattern requires digits on both sides of the point, unlike the more permissive float-number pattern
  • 3.14.15 fails because the anchors force the entire string to match, not just a leading substring
  • -0.0 matches even though it represents zero, since the pattern only checks shape, not numeric value
  • Trailing zeros like 3.140 match without issue since any number of fractional digits is allowed

Limitations

  • Does not accept scientific notation such as 1.5e10; use a dedicated scientific-notation pattern for that
  • Does not accept plain integers or values missing one side of the decimal point
  • Does not support thousands separators like commas within the integer part

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

3.14 -0.5 123.456

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const decimalPattern = /^-?\d+\.\d+$/;

function isDecimalNumber(value) {
  return decimalPattern.test(value);
}

console.log(isDecimalNumber("3.14")); // true

Common Mistakes

Using an unescaped `.` for the decimal point, which matches any character instead of a literal dot.

Fix: Escape the decimal point as `\.` so only a literal dot is accepted.

Expecting this pattern to also match plain integers like 42.

Fix: Use the separate integer pattern for whole numbers, or make the fractional group optional if both should be accepted.

Expecting shorthand forms like .5 or 3. to match.

Fix: Use the float-number pattern instead, which allows an omitted digit on one side of the point.

Performance Notes

  • Both `\d+` groups are non-overlapping and separated by a literal dot, so there is no catastrophic backtracking risk.
  • Anchoring both ends lets malformed input like extra trailing characters fail immediately.
  • For bulk validation, precompile the regex once and reuse the compiled object across calls.

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes