I am using this function:
function limitWords(id) {
var maxWords=3;
var d=document.getElementById(id);
if ( d.value.split(' ').length > maxWords ) {
t=d.value.substring(0,d.value.lastIndexOf(' '));
d.value=t.substring(0,t.lastIndexOf(' ')+1);
alert("You can choose up to 3 sectors");
}
}
Which I call like this:
<input type="text" name="et_newpost_tags" onkeyup="limitWords(this.id)"
I would also like to add the function which will format every word to title case. I found this function:
function toTitleCase(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
here: Convert string to title case with JavaScript
So I was wondering if it is possible to merge the second function into the first one, so the function would capitalize first letter of each word (separated by commas), without affecting the function which counts the number of commas in the input fields and puts an alert.