Numbers Only Regex: Match Digits Only in JavaScript and Python
Use this numbers only regex to validate digits-only strings, allow or disallow empty values, and add copy-ready examples for JavaScript, Python, and HTML forms.
Use this numbers only regex when a value must contain digits only, with no spaces, letters, signs, or separators. Below you will find the exact regex, copy-ready examples for JavaScript, Python, and HTML forms, plus common mistakes to avoid.
Numbers only regex
Use ^\d+$ to match a string that contains digits only from start to end.
Primary regex:
^\d+$What it matches:
- 7
- 12345
- 20260514
What it does not match:
- 12 345
- -42
- 3.14
- abc123
- 123abc
Quick note: If you want to allow an empty value, use ^\d*$ instead.
What does ^\d+$ mean?
This pattern checks whether the entire string contains one or more digits only:
^\d+$Explanation:
- ^ starts the match at the beginning of the string
- \d matches a digit from 0 to 9
- + requires one or more digits
- $ ends the match at the end of the string
This is the right pattern for values like IDs, counters, numeric form fields, and strings that must not contain spaces, letters, or punctuation.
Numbers only regex that allows an empty string
If an empty field should still be considered valid, use:
^\d*$This version matches:
- ``
- 5
- 123456
This version does not match:
- 12 34
- 12.5
- A12
JavaScript example
Use this regex when you want to validate a digits-only string:
const digitsOnlyRegex = /^\d+$/;
digitsOnlyRegex.test("123456"); // true
digitsOnlyRegex.test("00123"); // true
digitsOnlyRegex.test("12 34"); // false
digitsOnlyRegex.test("-42"); // false
digitsOnlyRegex.test("3.14"); // false
digitsOnlyRegex.test("abc123"); // falseIf the field is optional:
const optionalDigitsOnlyRegex = /^\d*$/;
optionalDigitsOnlyRegex.test(""); // true
optionalDigitsOnlyRegex.test("123456"); // true
optionalDigitsOnlyRegex.test("12-34"); // falsePython example
In Python, use a raw string and re.fullmatch() so the whole value must match:
import re
digits_only_regex = r"\d+"
bool(re.fullmatch(digits_only_regex, "123456")) # True
bool(re.fullmatch(digits_only_regex, "00123")) # True
bool(re.fullmatch(digits_only_regex, "12 34")) # False
bool(re.fullmatch(digits_only_regex, "-42")) # False
bool(re.fullmatch(digits_only_regex, "3.14")) # False
bool(re.fullmatch(digits_only_regex, "abc123")) # FalseOptional field example:
import re
optional_digits_only_regex = r"\d*"
bool(re.fullmatch(optional_digits_only_regex, "")) # True
bool(re.fullmatch(optional_digits_only_regex, "12345")) # True
bool(re.fullmatch(optional_digits_only_regex, "12-34")) # FalseHTML form validation example
If you want to validate digits only in a form field, use the pattern attribute:
<input
type="text"
name="employee_id"
inputmode="numeric"
pattern="\d+"
title="Please enter digits only"
/>Notes:
- inputmode="numeric" helps mobile users open a numeric keyboard
- pattern="\d+" validates one or more digits
- if the field is optional, do not add required
Extract digits from a longer string
If your input contains other text and you want to extract numeric parts instead of validating the entire value, use a different regex:
\d+JavaScript example:
"Order #584 shipped on 2026-05-14".match(/\d+/g);
// ["584", "2026", "05", "14"]Use ^\d+$ for validation and \d+ for extraction. They solve different problems.
Common mistakes
1. Forgetting ^ and $
This is one of the most common mistakes:
\d+This pattern finds digits anywhere inside a string, so it would match abc123xyz. If you need a digits-only value, use:
^\d+$2. Using the digits-only regex for decimals or negative numbers
This page is for integers written as digits only. It will not match:
- -42
- 3.14
- 1,000
- 12 345
If you need signed numbers, decimals, currency values, or formatted numbers, use a different pattern.
3. Accidentally allowing empty values
Use:
^\d+$when the field must contain at least one digit.
Use:
^\d*$only when an empty string is valid.
4. Expecting this regex to validate phone numbers
Phone numbers often include:
- a leading +
- spaces
- hyphens
- parentheses
That means a phone number regex should usually be different from a strict digits-only regex.
Below is a simple regular expression that allows validating if a given string contains only numbers:
True
False
Enter a text in the input above to see the result
Example code in Javascript:
Real number regex
Real number regex can be used to validate or exact real numbers from a string.
True
False
Enter a text in the input above to see the result
Example code in Javascript:
True
False
Enter a text in the input above to see the result
Notes on number only regex validation
In JavaScript you can also validate number by trying to parse it using a Number constructor and then using isNaN function of a Number class:
