Greetings, I want to capitalize each word in a script, for this I have came up with a such method:
//Word Capitalization
function wordToUpper(val) {
newVal = '';
val = val.toLowerCase().split(' ');
for(var c=0; c < val.length; c++) {
newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
}
return newVal;
}
Now it works for regular words starting after and emtpy char " ".
However I also want to make sure it fails for such strings:
wordToUpper('hello my name is Hellnar.it doesnt work.')
-> Hello My Name Is Hellnar.it Doesnt Work.
"it" had to be capital.
wordToUpper('hello my name is (hellnar).')
-> Hello My Name Is (hellnar).
"Hellnar" had to capital.
Regards
Note: Please not the css classic text-transform: capitalize;
solution as this data will be used for form posting.