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.

Community
  • 1
  • 1
Bruno Kos
  • 645
  • 2
  • 8
  • 18
  • Limit words seems to already contain a toProperCase function which will be on every string. – Mark Broadhurst Aug 09 '12 at 09: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. – Bruno Kos Aug 09 '12 at 12:08

2 Answers2

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.

James Hill
  • 60,353
  • 20
  • 145
  • 161
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.

Mark Broadhurst
  • 2,675
  • 23
  • 45
  • This one works exactly as it is supposed to work, changing the capitalization in real time. – Bruno Kos Aug 09 '12 at 13:46
  • 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. – Bruno Kos Aug 09 '12 at 13:49
  • Swap "if (d.value.split(' ').length > maxWords) {" for "if (d.value.split(',').length > maxWords) {". if this answer works for you can you choose it as the answer ? – Mark Broadhurst Aug 09 '12 at 13:50
  • Saint, all your solutions worked for me, thank you very much. I am happy to choose it as the answer. I cannot vote up, though, as I get "15 reputation required" box. I am new here, that's why. Thank you very much again. – Bruno Kos Aug 09 '12 at 16:40
  • I've just noticed another issue here - If I don't put space after the comma, the function does not work properly, meaning it does not uppercase other words, only the first one. So, the function would do this: "One,two,three" and if I add spaces, the result would be "One, Two, Three". Any ideas on how ti fix this? – Bruno Kos Aug 20 '12 at 10:02
  • 1
    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 ? – Mark Broadhurst Aug 20 '12 at 10:28
  • 1
    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). – Mark Broadhurst Aug 20 '12 at 10:32
  • I would like to have "," as sector separator, while inside every sector (max 3), the user can put multiple words (something like This is first sector, this is the second, etc). – Bruno Kos Aug 20 '12 at 15:18
  • 1
    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" ? – Mark Broadhurst Aug 20 '12 at 16:03
  • I would like each word capitalized. It should say "This Is First Sector, This Is The Second", but it should work if the user don't put space after coma as well, so this should be the result in that case: "This Is First Sector,This Is The Second" – Bruno Kos Aug 20 '12 at 18:21