-4

I have made a form for billing, but am having trouble converting the first letter entered in a text box to capital. I've looked around a bit, but couldn't find any luck.

Basically what I'm looking for is if possible the second I enter a letter into say first name text box it auto sets capital (if user forgets to capitalize) and then the rest are in lowercase.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193

3 Answers3

1

If this is a visual need only, it can be done with css:

input {
   text-transform:capitalize;
}
Damien Black
  • 5,579
  • 18
  • 24
  • since he need it in a textbox i believe he is going to save those in db. so visual won't be enough. – Volkan Ulukut Feb 05 '14 at 22:33
  • This is the only one that "Convert first letter of each word entered in a textbox to captial" so far, even if not useful for the OP's situation. – Xotic750 Feb 05 '14 at 22:45
1

check this jsfiddle: http://jsfiddle.net/7zp6k/

this function should do it:

function capitalize(obj)
{
    obj.value = obj.value.charAt(0).toUpperCase() + obj.value.slice(1);
}

html:

<input type='text' id='textfield' onkeyup='capitalize(this)'>
Volkan Ulukut
  • 4,230
  • 1
  • 20
  • 38
  • Yup - saw your edit, tried your jsfiddle, and up voted when I saw it worked. Then I deleted my comment saying you hadn't linked to the right thing. – ArtOfWarfare Feb 05 '14 at 22:30
  • I'm trying this out in jsfiddle, but when I type in john it doesn't convert the first letter (in this case J), which is what I'm looking for if possible. Unless I'm doing this in the wrong section. – Winter is Coming Feb 05 '14 at 22:36
  • It's working. you'll type it in the texfield on bottom right corner. or better yet, try it on your own code. – Volkan Ulukut Feb 05 '14 at 22:37
  • It does work. I wasn't signed in thanks a bunch really helpful code – Winter is Coming Feb 05 '14 at 22:43
  • accepting my answer would help others as well ;) – Volkan Ulukut Feb 05 '14 at 22:47
  • This doesn't "Convert first letter of each word entered in a textbox to captial". – Xotic750 Feb 05 '14 at 22:56
  • he wasn't asking for that. "Basically what I'm looking for is if possible the second I enter a letter into say first name text box it auto sets capital (if user forgets to capitalize) and then the rest are in lowercase." – Volkan Ulukut Feb 05 '14 at 23:02
  • His title also says "Convert first letter of each word entered in a textbox to captial", so perhaps you could have clarified the OP's requirements in a comment, before posting an answer. If I had searched for such a solution and find your answer, then I have wasted my time because it would not have worked as I had expected it to. Basically, make sure the question is a good one first. – Xotic750 Feb 05 '14 at 23:08
-1

Here's another way.

Just use jQuery and a capitalize function.

Here is a working example.

$("#firstName").keyup(function(event) {
  var tempVal = $("#firstName").val();
  $("#firstName").val(capitaliseFirstLetter(tempVal));
});
BenMorel
  • 34,448
  • 50
  • 182
  • 322
chonerman
  • 117
  • 11
  • Can you explain to me why this was rated down? It does exactly what was asked... – chonerman Feb 05 '14 at 22:47
  • Probably because you use jQuery, which is not specified or even mentioned, without demonstrating a Javascript equivalent. You also do not show the `capitalizeFirstLetter` function which is vital to the question, the jQuery isn't. And finally it doesn't "Convert first letter of each word entered in a textbox to captial" – Xotic750 Feb 05 '14 at 22:52
  • That's funny. The original question doesn't specify javascript only. The captializeFirstLetter function is in the working example jfiddle. You got me on the last one! =) However, to my credit reading the description of the original question they are looking for the "first letter entered in a text box to capital". – chonerman Feb 06 '14 at 20:39
  • 1
    "JavaScript is a dynamically-typed language commonly used for client-side scripting. Use this tag for questions regarding ECMAScript and its dialects/implementations (excluding ActionScript). Unless a tag for a framework/library is also included, a pure JavaScript answer is expected." – Xotic750 Feb 06 '14 at 20:55
  • And what if jsFiddle is down, or the example gets deleted? Then it is no longer available. And the title, "Convert first letter of each word entered in a textbox to captial" seems pretty clear to me, in Swedish, first names can be multiples, so "Sven Göran", why would you want it to be "Sven göran"?. – Xotic750 Feb 06 '14 at 20:56