Validation
Decimal Number Regex
Prices, measurements and coordinates are often typed as decimal numbers. This pattern allows an optional leading minus sign, requires at least one digit, and then allows an optional fraction made of a dot followed by one or more digits. Inputs like 3. or .5 are rejected because one side of the point is empty, which keeps the format predictable for parsers and databases. Thousands separators and exponents are not accepted. If your users type commas as decimal separators, as is common in many European countries, swap the dot for a class such as [.,] and convert before parsing.
Pattern (JavaScript)
/^-?\d+(\.\d+)?$/Starts at the start of the line/string, then the character "-", optionally (zero or one time), then one or more digits, then a group (the character ".", then one or more digits), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ 3.14
- ✓ -0.5
- ✓ 42
Does not match
- ✗ 3.
- ✗ .5
- ✗ 1,000
Try it
0 matches (JavaScript engine)
3.14
-0.5
42
3.
.5
1,000Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^-?\d+(\.\d+)?$/g;
const text = "3.14";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^-?\d+(\.\d+)?$')
text = '3.14'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^-?\d+(\.\d+)?$/';
$text = '3.14';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
String text = "3.14";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^-?\d+(\.\d+)?$`)
text := "3.14"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^-?\d+(\.\d+)?$");
var text = "3.14";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^-?\d+(\.\d+)?$").unwrap();
let text = "3.14";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^-?\d+(\.\d+)?$/
text = '3.14'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I limit the number of decimal places?
- Replace \d+ in the fraction with \d{1,2} for up to two places. That suits money values in most currencies.
- Why is .5 rejected?
- The pattern requires at least one digit before the point. Use ^-?(\d+(\.\d*)?|\.\d+)$ if you want to accept forms like .5 and 5.
- Does it accept a plus sign?
- No. Only an optional minus is allowed. Change -? to [+-]? to accept an explicit plus sign too.