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()" >