How do I make an input box accept numbers only?

I would just go with the HTML standard, as @Kevin is suggesting. You could use the min and max attributes to set a reasonable age span. Don’t like the scrollbars? Turn off the number input spinners.

But if you are going the custom way… Your isNumber function right now accepts input other than numbers.

  1. Enter 12.
  2. Go left one step (set the character input between 1 and 2).
  3. Input anything, like letters.

Maybe you could do something like this instead?

function setup() {
  let age = createInput('');
  age.input(enforcePositiveInteger);
}

function enforcePositiveInteger() {
  const userInput = this.value();
  const onlyDigits = userInput.replace(/^0+|[^\d]/g, '');
  
  if (userInput !== onlyDigits) {
    this.value(onlyDigits);
  }
}
2 Likes