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.
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()" >
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.
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.
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.
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.
You can add an eventlistener, then test the number of words.
onkeydown
// 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>
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With