# Regex for Numbers Only (Digits)

> The regex for numbers only is ^\d+$ — one or more digits and nothing else. Variations for fixed lengths, empty strings and ASCII-only digits, explained.

```
/^\d+$/
```

^\d+$ accepts a string made entirely of digits: no spaces, signs, decimal points or letters. The anchors are what make it 'only' — without them, \d+ would happily find the 123 inside abc123.

## Token by token

| Token | Meaning |
|---|---|
| `^` | start of string |
| `\d+` | one or more digits (0–9) |
| `$` | end of string — nothing else may follow |

## Examples

- ✓ matches: `0`
- ✓ matches: `42`
- ✓ matches: `0012345`
- ✗ does not match: ``
- ✗ does not match: `12a`
- ✗ does not match: `-5`
- ✗ does not match: `3.14`
- ✗ does not match: `1 000`

## Variations

- `^\d{4}$` — exactly four digits, such as a PIN or a year
- `^\d*$` — digits or an empty string
- `^[0-9]+$` — ASCII digits only, in every language

## Language notes

- In Python 3 and .NET, \d also matches other scripts' digits, like ٣ or ३. Use [0-9], or Python's re.ASCII flag, to accept only 0–9.

## FAQ

### How do I allow negative numbers?

Add an optional sign: ^-?\d+$. For decimals as well, see the decimal number pattern.

### Should I validate numbers with regex at all?

For form input it's fine. If you need the value, parse it (Number.parseInt, int()) and check its range too — a regex can't tell you a number is under 1,000.

## Related patterns

- [Regex for Decimal Numbers](https://www.devkult.com/regex/decimal-number.md)
- [Regex for Alphanumeric Strings](https://www.devkult.com/regex/alphanumeric.md)
- [Regex for US ZIP Codes](https://www.devkult.com/regex/us-zip-code.md)

Open the interactive page: https://www.devkult.com/regex/digits-only