Horje
html input max words Code Example
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><pre class="easycode; title:;lang:;"><xmp data-max-words="100" rows="5" style="width:100%" data-announce="true">

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'))
  })
})




Html

Related
display pdf in html Code Example display pdf in html Code Example
date month year dropdown html Code Example date month year dropdown html Code Example
js coding practices airbnb Code Example js coding practices airbnb Code Example
how to put social media icons in html Code Example how to put social media icons in html Code Example
Uncaught TypeError: Cannot read properties of undefined (reading 'mData') Code Example Uncaught TypeError: Cannot read properties of undefined (reading 'mData') Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9