Is it possible to set a maximum word limit for a textbox that a user can input, rather than limiting the number of characters?
I did some research and discovered a way to determine the number of words a user has entered using regular expressions, but I'm unsure how to prevent them from entering more words once the limit is reached.
var jobValue = document.getElementsById('textBox1').value
var words = jobValue.value.match(/\S+/g).length;
if(words>=2){
//stop inputs
}
PS.
I want to restrict 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()" >