0

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.

2
  • Limit words seems to already contain a toProperCase function which will be on every string. Commented Aug 9, 2012 at 9:50
  • Oh, I am sorry, I haven't noticed I put my custom function (merged) which didn't work. I have updated the original question (first code block) now. Commented Aug 9, 2012 at 12:08

2 Answers 2

1
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");
    }

    // Ensure title case
    d.value = toTitleCase(d.value);
}

function toTitleCase(str) {
    return str.replace(/\w\S*/g, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}​
​

Here's a working fiddle.

Note: I did not change the functionality of your existing limitWords function, assuming it was working the way you intended.

Sign up to request clarification or add additional context in comments.

4 Comments

That would only title case when the content was over the limit.
Exactly, this one works, but it triggers only if I put more than three words - changes the capitalization after the alert box.
@James Hill nice correct your answer and downvote my answer thats really classy. Even after I credit that its is basically your answer.
Thank you James very much as well, all the help appreciated.
0
function limitWords(id) {
var maxWords = 3;
var d = document.getElementById(id);
if (d.value.split(' ').length > maxWords) { // Swap ' ' for ',' if you want comma's
    t = d.value.substring(0, d.value.lastIndexOf(' '));
    d.value = t.substring(0, t.lastIndexOf(' ') + 1);

    alert("You can choose up to 3 sectors");
}
// Ensure title case
d.value = toTitleCase(d.value);
}

function toTitleCase(str) {
    return str.replace(/\w\S*/g, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}​

Basically James hills answer but with the "toTitleCase" call in the right place.

10 Comments

This one works exactly as it is supposed to work, changing the capitalization in real time.
There is one more thing I've just noticed, not sure if it can be included in this thread. My alert shows up after three words, not three comas. Meaning, if the first "word" consists of two words, it will not allow me to put more than one real item. Any ideas for that? So, actually, I would like to be able to put "one, two, three", and at the moment, it would allow me "one two, three" only.
Your regex is looking for spaces if you dont have any then it will not split the words up correctly and not capitalise them. What characters do you want to accept as words and which characters do you want to accept as parts of words and which do you want to have as word separators ?
Sorry didnt answer your question the regex contains "\w\S" meaning letters digits and underscore (\w) followed by negated whitespace or anything that is not whitespace (\S).
So do you want each word capitalized or just the first of each token i.e. with the entry of "This is first sector, this is the second" should it say "This Is First Sector, This Is The Second" or "This is first sector, This is the second" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.