Back to Home

Decimal Number Validation Regex

CronOS Team
regexdecimalvalidationtutorialnumberfloat

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

Decimal Number Validation Regex

Validate decimal numbers (floating point numbers) with optional minus sign and decimal point using regex pattern.

Pattern Breakdown

regex
^-?\d+(\.\d+)?$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
-?Optional minusOptional minus sign
\d+Integer partOne or more digits
(\.\d+)?Optional decimal partOptional dot and one or more digits
$End anchorEnsures match to string end

Character Classes

  • -? - Optional minus sign
  • \d+ - One or more digits
  • (\.\d+)? - Optional decimal part:
    • \. - Literal dot (escaped)
    • \d+ - One or more digits after decimal
    • ? - Makes entire group optional

Examples

Valid:

  • 123
  • 123.45
  • -123
  • -123.45
  • 0.5
  • -0.5
  • 999.999

Invalid:

  • .5 (missing integer part)
  • 123. (incomplete decimal)
  • 12.34.56 (multiple decimal points)
  • +5.5 (plus sign)
  • 123.45 (leading space)
  • abc (non-numeric)

Implementation

JavaScript

javascript
const decimalRegex = /^-?\d+(\.\d+)?$/;
decimalRegex.test('123.45'); // true
decimalRegex.test('-123.45'); // true
decimalRegex.test('123'); // true (integer also valid)
decimalRegex.test('.5'); // false (missing integer part)
decimalRegex.test('123.'); // false (incomplete decimal)

Python

python
import re
decimal_regex = r'^-?\d+(\.\d+)?$'
bool(re.match(decimal_regex, '123.45'))  # True
bool(re.match(decimal_regex, '-123.45'))  # True
bool(re.match(decimal_regex, '123'))  # True (integer also valid)
bool(re.match(decimal_regex, '.5'))  # False (missing integer part)

Go

go
decimalRegex := regexp.MustCompile(`^-?\d+(\.\d+)?$`)
decimalRegex.MatchString("123.45") // true
decimalRegex.MatchString("-123.45") // true
decimalRegex.MatchString("123") // true (integer also valid)
decimalRegex.MatchString(".5") // false (missing integer part)

Limitations

  1. Accepts integers: Also matches integers (e.g., 123)
  2. No scientific notation: Doesn't support 1.23e5 format
  3. No leading zeros: Accepts 0123.45 (may want to prevent this)
  4. No precision limit: Doesn't limit decimal places
  5. String validation: Validates format, not actual float type

When to Use

  • Decimal number input validation
  • Price/currency input (before formatting)
  • Measurement values
  • When you need flexible number validation
  • Form input validation

For production, consider:

  • Requiring decimal part if needed: ^-?\d+\.\d+$
  • Supporting scientific notation if needed
  • Adding precision limits
  • Converting to float type after validation
  • Handling locale-specific decimal separators (comma vs dot)

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern