Search
 
SCRIPT & CODE EXAMPLE
 

HTML

html input max words

<p><input type="text" data-max-words="2" data-announce="true"></p>
<p><input type="text" data-max-words="3"></p>
<p><input type="text" data-max-words="4"></p>
<p><textarea data-max-words="100" rows="5" style="width:100%" data-announce="true"></textarea></p>
Comment

html input max words

// Get all inputs that have a word limit
document.querySelectorAll('input[data-max-words]').forEach(input => {
  // Remember the word limit for the current input
  let maxWords = parseInt(input.getAttribute('data-max-words') || 0)
  // Add an eventlistener to test for key inputs
  input.addEventListener('keydown', e => {
    let target = e.currentTarget
    // Split the text in the input and get the current number of words
    let words = target.value.split(/s+/).length
    // If the word count is > than the max amount and a space is pressed
    // Don't allow for the space to be inserted
    if (!target.getAttribute('data-announce'))
      // Note: this is a shorthand if statement
      // If the first two tests fail allow the key to be inserted
      // Otherwise we prevent the default from happening
      words >= maxWords && e.keyCode == 32 && e.preventDefault()
    else
      words >= maxWords && e.keyCode == 32 && (e.preventDefault() || alert('Word Limit Reached'))
  })
})
Comment

PREVIOUS NEXT
Code Example
Html :: how to show a html page only once 
Html :: exemple liens hypertextes 
Html :: color ful scroll bar 
Html :: CAMBIAR COLOR AA UN ICON SGV EN HTML 
Html :: php if statement not working 
Html :: function on html button b 
Html :: &#47;&gt; in html 
Html :: Grepper Alt+G 
Html :: how to embed mp4 html 
Html :: redirect user when want to access html page 
Html :: pulsating icons 
Html :: save base 64 string to file 
Html :: <model-view in html tag 
Html :: html role for icon 
Html :: how to add multiple rows inside a row in html 
Html :: html5 vs css3 
Html :: DOM-LEVEL-2-CORE 
Html :: block iframe pubblicity 
Html :: how to remove whitespace below my footer in html 
Html :: how to display java code in html 
Html :: in line a frame animation 
Html :: chrome youtube tab goes blank 
Html :: how set for loop in html tag with jquery 
Html :: h2 html 
Html :: h1 tag is used for 
Css :: disable drag image css 
Css :: media query max and min width 
Css :: how to contain an image within a div 
Css :: css grid two columns 
Css :: css remove link border on click 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =