Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set maximum number of words HTML textbox

I want to set a maximum number of words the user can type in a textbox, not the number of character but words.
This is possible?

I did some digging and found out how to get the number of words the user has inputted using regular expressions, but im not sure how to stop the user from entering more letters after reaching the max

var jobValue = document.getElementsById('textBox1').value
var words = jobValue.value.match(/\S+/g).length;
if(words>=2){
   //stop inputs
}

PS.
I want to limit the number of words to 2.

UPDATE

function wordLimit(){
    
       var jobValue = document.getElementById('wordIntent').value
       var words = jobValue.value.split(/\s+/);
       var maxWords = 2; 
       var numWords = words.length;
       if(numWords > maxWords){
    		  jobValue.preventDefault(); 
        }
}
<input type="text" id="wordIntent" onkeydown="wordLimit()" > 
like image 760
Elinoter99 Avatar asked Nov 06 '17 19:11

Elinoter99


People also ask

How do you set a max limit in HTML?

Given an input field and the task is to set the minimum/maximum number of characters allowed in an input field in HTML. To set the maximum character limit in input field, we use <input> maxlength attribute. This attribute is used to specify the maximum number of characters enters into the <input> element.

How do I limit the length of a word in HTML?

To add a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. To limit the number of characters entered in a textarea, use the maxlength attribute. The value if the attribute is in number.

How do you set the minimum and maximum length of a textbox in HTML?

You can specify a minimum length (in characters) for the entered value using the minlength attribute; similarly, use maxlength to set the maximum length of the entered value, in characters. The example below requires that the entered value be 4–8 characters in length.

How do I limit characters in a textbox?

Right-click the text box for which you want to limit characters, and then click Text Box Properties on the shortcut menu. Click the Display tab. Under Options, select the Limit text box to check box, and then specify the number of characters that you want.


2 Answers

You can add an eventlistener, then test the number of words.

  • Find all inputs that have a word limit
  • Iterate through each item in the list
  • Add an event listener to each item
  • Find the number of words in the input with onkeydown
  • If we have reached the max number of words don't allow for any more spaces
  • Otherwise we can allow for other characters

// 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'))
  })
})
<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>
like image 67
Get Off My Lawn Avatar answered Oct 07 '22 05:10

Get Off My Lawn


You can just get the value of the textbox and then split that into an array where there are spaces and then check how many items are in the array:

// 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
  }
});
<input type="text" id="input">
like image 20
Scott Marcus Avatar answered Oct 07 '22 06:10

Scott Marcus