Batas input JS HTML menjadi 5 kata

// Add event handler for event that can be cancelled and prevent excessive data
// from ever getting into the textbox
document.getElementById("input").addEventListener("keypress", function(evt){

  // Get value of textbox and split into array where there is one or more continous spaces
  var words = this.value.split(/\s+/);
  var numWords = words.length;    // Get # of words in array
  var maxWords = 2;
  
  // If we are at the limit and the key pressed wasn't BACKSPACE or DELETE,
  // don't allow any more input
  if(numWords > maxWords){
    evt.preventDefault(); // Cancel event
  }
});
Weary Wildebeest