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!
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
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
-? | Optional minus | Optional minus sign |
\d+ | Integer part | One or more digits |
(\.\d+)? | Optional decimal part | Optional dot and one or more digits |
$ | End anchor | Ensures 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:
123123.45-123-123.450.5-0.5999.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
- Accepts integers: Also matches integers (e.g.,
123) - No scientific notation: Doesn't support
1.23e5format - No leading zeros: Accepts
0123.45(may want to prevent this) - No precision limit: Doesn't limit decimal places
- 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!