Managed IT · October 6, 2026 · intSignal Team

The Number Field Nobody Could Edit: Why Clamping on Every Keystroke Breaks Input

Share this article

A customer told us the user-count field on our pricing form was broken. Not slow, not wrong — impossible to edit. With the value at 1, pressing backspace did nothing. The 1 was still there. You could not type 25 because you could never get rid of the digit already in the box.

They were right, and the bug is one of the most common in form code:

<input
  type="number"
  value={seats}
  onChange={(e) => setSeats(clampSeats(e.target.value))}
/>

with the entirely sensible-looking helper:

function clampSeats(v) {
  const n = Math.floor(Number(v));
  if (!Number.isFinite(n)) return 1;
  return Math.min(10_000, Math.max(1, n));
}

Trace a single keystroke

The person presses backspace on a field containing 1.

  1. The input's value becomes "".
  2. onChange fires with e.target.value === "".
  3. Number("") is 0. Math.floor(0) is 0. Number.isFinite(0) is true.
  4. So the guard does not fire, and Math.max(1, 0) returns 1.
  5. React re-renders with value={1}.

The digit reappears faster than the screen can show it gone. The field is now a prison: the only reachable states are the ones your clamp considers valid, and every intermediate state a human must pass through on the way to typing a number is treated as invalid and corrected.

The same bug produces other symptoms depending on the clamp:

  • a field with minimum 10 that snaps to 10 as soon as you type 2 on the way to 25
  • a currency field that rewrites 1. to 1 so the decimal point can never be typed
  • a field that jumps the cursor to the end after every character, because the value was replaced

An empty field is not an invalid number. It is an unfinished one. Those need different handling.

The fix: separate what is typed from what is committed

Keep two pieces of state. The text is exactly what the person has typed and may be empty or partial. The committed number is the last good value, and it is what the rest of the application uses.

export function seatsFromInput(raw, current) {
  const text = String(raw ?? "").replace(/\D/g, "").slice(0, 5);
  return { text, seats: text === "" ? clampSeats(current) : clampSeats(text) };
}

export const seatsOnBlur = (text, current) =>
  String(text.trim() === "" ? clampSeats(current) : clampSeats(text));

Three decisions are doing the work:

  • The text is never rewritten mid-edit. Strip characters that can never be valid (anything non-numeric) and cap the length so the field cannot be flooded, but otherwise leave what was typed alone. Typing 0 shows 0, even though the committed value is 1.
  • An empty field keeps the last good number. The price on screen does not blank out or flash to zero while someone is mid-edit. Nothing downstream ever receives an empty value.
  • Normalising happens on blur. When the person leaves the field, whatever is there settles to a usable number. That is the moment they have finished, and the only moment correction is not interference.

Wiring it up:

<input
  type="text"
  inputMode="numeric"
  value={text}
  onChange={(e) => {
    const next = seatsFromInput(e.target.value, seats);
    setText(next.text);
    onChange(next.seats);
  }}
  onBlur={() => setText(seatsOnBlur(text, seats))}
/>

Note type="text" with inputMode="numeric" rather than type="number". You get the numeric keypad on mobile without the spinner, without browsers silently reporting "" for values they consider invalid, and without locale-dependent parsing surprises.

While you are there: pair it with a slider

Typing is precise; dragging is fast. Driving both from the same committed number gives you each without a second source of truth:

<input
  type="range"
  min={1}
  max={MAX}
  value={Math.min(seats, MAX)}
  onChange={(e) => { const n = clampSeats(e.target.value); setText(String(n)); onChange(n); }}
/>

Math.min(seats, MAX) matters: if a larger number was typed than the bar covers, the thumb pins at the end rather than React and the browser disagreeing about the value.

If an external change can also move the value — a deep link, another control on the page — sync the text back, but never while the field has focus:

useEffect(() => {
  if (document.activeElement?.id !== id) setText(String(seats));
}, [seats, id]);

Without that guard you have reinvented the original bug by a different route.

Tests worth writing

The one that would have caught the reported bug reproduces the exact sequence:

test("typing 1 then clearing then typing 25 works", () => {
  let s = { text: "1", seats: 1 };
  s = seatsFromInput("", s.seats);     // backspace
  s = seatsFromInput("2", s.seats);
  s = seatsFromInput("25", s.seats);
  assert.equal(s.text, "25");
  assert.equal(s.seats, 25);
});

Plus the properties that keep it fixed:

assert.equal(seatsFromInput("", 1).text, "");       // clearing is allowed
assert.equal(seatsFromInput("", 1).seats, 1);       // pricing still has a number
assert.equal(seatsFromInput("0", 5).text, "0");     // partial entry not rewritten
assert.equal(seatsOnBlur("", 7), "7");              // settles on leaving
assert.equal(seatsOnBlur("0", 7), "1");             // junk settles to something usable

When we deliberately reintroduced the old behaviour — making seatsFromInput return String(clampSeats(text)) as the text — the first test failed immediately. That is the regression guard that matters, because this bug is easy to reintroduce: clamping on change looks more careful than clamping on blur.

The general rule

Validate on the boundary the person controls, not on the one the framework gives you. onChange fires on every keystroke; a human finishes a number when they stop typing. Correcting input between those two moments is not validation — it is fighting the person using your form, and they will lose.

Share this article