JSFiddle - React, Tailwind, and code Playground

HTML

<form action="/action_page.php">
  mynumber: <input type="text" maxlength="4" id="myId"/>
  <input type="submit" value="Submit">
</form>

JavaScript

var input = document.getElementById('myId');

input.addEventListener('keyup', function (e) {
  // Checking length of string ignoring all non-digit and non-letter characters
  if (this.value.replace(/[^\d|[^a-zA-Z]]*/g, '').length == 3) { 
    console.log('stop and do whatever you need Venky')
    e.preventDefault();
  }
})