Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js html input limit to 5 words

// 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
  }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: svg to png base64 javascript 
Javascript :: sequelize include twice 
Javascript :: moment format heure 
Javascript :: body-parser is depreciated 
Javascript :: change terminal shortcut vscode 
Javascript :: make an object javascript 
Javascript :: String variable props 
Javascript :: react fragment inside map with key prop 
Javascript :: [W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]). 
Javascript :: moment clone 
Javascript :: play notification sound on chat js 
Javascript :: get filenem js 
Javascript :: path module js 
Javascript :: get 2nd td of tr 
Javascript :: js highest number in array 
Javascript :: get before 6 month date javascript node js 
Javascript :: js string to boolean 
Javascript :: js iife 
Javascript :: js array random 
Javascript :: js setattribute download 
Javascript :: clone element 
Javascript :: ejs formatter vscode 
Javascript :: how to get java model attributes from javascript 
Javascript :: using template literals to create html 
Javascript :: display for sometime only session flash message in laravel with javascript 
Javascript :: template literals javascript 
Javascript :: vue function data update 
Javascript :: Convert array to string while preserving brackets 
Javascript :: update a certain key in dictionary javascript 
Javascript :: javaScript Option to deactivate all bs.tooltip on document 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =