2

How do I capitalize the first character of each word while typing in Drupal 7?

I mean, while creating a node and entering the node title, while typing something it capitalizes the first character of each word.

I know that it does this capitalization with JavaScript and I found some JavaScript function like that:

function ucFirstAllWords( str )
{
    var pieces = str.split(" ");
    for ( var i = 0; i < pieces.length; i++ )
    {
        var j = pieces[i].charAt(0).toUpperCase();
        pieces[i] = j + pieces[i].substr(1);
    }
    return pieces.join(" ");
}

How do I apply this on Drupal for example on page.tpl.php file and/or for CCK fields.

NOTE 1: I don't want to use css, because it's just for display.

NOTE 2: I don't want to use PHP ucwords() because it doesn't affect database saving.

Some more info about what I do not want to use:

  • I don't want to use PHP function ucwords(), I have to use PHP because it's Drupal.
  • I don't want to use CSS because what CSS does is display change, it doesn't change the actual value. And I don't want to use the ucwords() function because it doesn't affect the database. If I use ucwords(), yes it works on display (on html page) but it doesn't work for database saving. If I type 'heLLo world' it shows 'Hello World' but in database it stored as it typed 'heLLo world'.
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
herci
  • 375
  • 1
  • 4
  • 24
  • If you don't want to use php, why tag the question with php ?? –  Feb 10 '14 at 20:18
  • Your notes don't make a lot of sense to me, if it is just for display, you should use css. The second one I just don't get. – jeroen Feb 10 '14 at 20:18
  • if you apply `ucwords()` before putting the data in to the db, then wouldn't that work ? –  Feb 10 '14 at 20:27
  • @herci That totally depends on where you use `ucwords()`, on the input or on the output... – jeroen Feb 10 '14 at 20:28
  • @Dagon I tried this and I'm still trying to find where to put this function, thanks. – herci Feb 10 '14 at 20:53
  • 1
    I don't understand what is NOT clear in this question (= the reason why it got closed). I hope my additions via the most recent (suggested) edit are sufficient to get this question voted to be reopened ... Fingers crossed! – Pierre.Vriens Jan 20 '16 at 10:43

2 Answers2

2

Why do it with PHP or JS? Just use plain CSS:

.text_element { text-transform:capitalize; }
display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
0

Maybe better approach is to simply try some clientside validation modules. Examples:

dubwise
  • 21
  • 3
  • Thanks, I have already used Field Validation module but it validates. If a user doesn't type 'the right' thing, it just doesn't accept not convert the value. – herci Feb 11 '14 at 20:16