/^/
EasyNumbers3 min read

Integer Regex

Validates a whole number with an optional leading minus sign, disallowing decimal points and unnecessary leading zeros.

#integer#numbers#validation#whole-number#forms

Regex Pattern

^-?(0|[1-9]\d*)$

Pattern Breakdown

Hover over a token to see what it does.

^-?(0|[1-9]\d*)$
TokenMeaning
^Anchors the match to the start of the string.
-?An optional leading minus sign for negative integers.
0Matches the single digit zero on its own.
[1-9]\d*A non-zero digit followed by any number of digits, so multi-digit numbers can't start with 0.
$Anchors the match to the end of the string.

Detailed Explanation

What it does

This pattern matches strings representing a whole number, optionally negative, with no decimal point. It accepts a bare 0, or a non-zero digit followed by any further digits, so values like 42, -17, and 0 all match, while 007 and 3.14 do not.

Why it works

The alternation `(0|[1-9]\d*)` separates the single-character case of zero from multi-digit numbers, which must start with a non-zero digit. This prevents malformed leading-zero values like 007 from matching while still allowing arbitrarily long numbers such as 123456. The optional `-?` at the start handles negative integers without allowing a stray minus sign elsewhere in the string.

Common use cases

  • Validating quantity or count fields in a form that should only accept whole numbers
  • Filtering CSV columns or query parameters that must be plain integers
  • Rejecting decimal or malformed numeric input before parsing with `parseInt` or similar
  • Validating page numbers, IDs, or array indices in an API request

Edge cases

  • -0 matches this pattern since the sign and the digit 0 are both individually valid, even though it's a slightly unusual value
  • 007 correctly fails because leading zeros are disallowed for multi-digit numbers
  • An empty string fails because at least one digit is required
  • A number with surrounding whitespace, like ' 5', fails because the anchors require the whole string to be digits

Limitations

  • Does not support a leading plus sign for explicitly positive integers (e.g. +5)
  • Does not enforce any minimum or maximum value range, only the textual shape
  • Does not support thousands separators like commas or underscores

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

0 42 -17

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const integerPattern = /^-?(0|[1-9]\d*)$/;

function isInteger(value) {
  return integerPattern.test(value);
}

console.log(isInteger("-17")); // true

Common Mistakes

Using a naive `^-?\d+$`, which accepts leading zeros like 007 that most systems consider malformed.

Fix: Use the `(0|[1-9]\d*)` alternation to allow a bare zero but forbid leading zeros on longer numbers.

Forgetting the optional minus sign, so valid negative integers get rejected.

Fix: Add `-?` right after the start anchor to allow an optional leading minus.

Relying on this regex alone to guard against integer overflow in a fixed-size type.

Fix: After the format passes, parse the value and check it against the target type's min/max range.

Performance Notes

  • The pattern has no nested or overlapping quantifiers, so it runs in linear time with no catastrophic backtracking risk.
  • Anchoring both ends lets non-numeric input fail immediately without scanning further.
  • For very large numbers of validations, precompile the regex once and reuse it rather than recreating it per call.

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes