57

My goal is to use the jQuery event .keyup() to convert inputted lowercase chars to uppercase.

How can I achieve this?

Zuul
  • 16,217
  • 6
  • 61
  • 88
Jennifer Anthony
  • 2,227
  • 10
  • 37
  • 56
  • 7
    If you set the font for the input to an all caps font, you won't have to perform a conversion, and your users won't have to bother with typing in caps. You can easily perform a conversion to upper case in code if you want to save the data. – DOK Oct 10 '11 at 18:41
  • i recommend to use `oninput` event instead of keyup to avoid delayed process – Strywyr Feb 17 '23 at 07:37

13 Answers13

101

Plain ol' javascript:

var input = document.getElementById('inputID');

input.onkeyup = function(){
    this.value = this.value.toUpperCase();
}

Javascript with jQuery:

$('#inputID').keyup(function(){
    this.value = this.value.toUpperCase();
});
Andy Jones
  • 6,205
  • 4
  • 31
  • 47
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 4
    I also found this works: $(this).val($(this).val().toUpperCase()); – mattdlockyer Mar 21 '13 at 14:48
  • 11
    @mattdlockyer , it's so stupid that there should be a law against it on the general principle – tereško Mar 21 '13 at 15:26
  • This does NOT work. If you do testing in the DOM you will see the value is still lowercase. See @JLevett `$(this).val($(this).val().toUpperCase());` works – Ravi Ram Feb 06 '15 at 18:52
  • @DavidKEgghead the `.val` function does not change the attribute in the DOM's value, that always stays the same, it changes the DOM property You _might_ want to read: http://stackoverflow.com/q/5874652/561731 – Naftali Feb 09 '15 at 16:57
39

The only issue with changing user input on the fly like this is how disconcerting it can look to the end user (they'll briefly see the lowercase chars jump to uppercase).

What you may want to consider instead is applying the following CSS style to the input field:

text-transform: uppercase;

That way, any text entered always appears in uppercase. The only drawback is that this is a purely visual change - the value of the input control (when viewed in the code behind) will retain the case as it was originally entered.

Simple to get around this though, force the input val() .toUpperCase(); then you've got the best of both worlds.

Conan
  • 2,659
  • 17
  • 24
4

Above answers are pretty good, just wanted to add short form for this

 <input type="text" name="input_text"  onKeyUP="this.value = this.value.toUpperCase();">
mokNathal
  • 533
  • 1
  • 6
  • 20
4
$(document).ready(function()
{
    $('#yourtext').keyup(function()
    {
        $(this).val($(this).val().toUpperCase());
    });
});

<textarea id="yourtext" rows="5" cols="20"></textarea>
JConstantine
  • 3,980
  • 1
  • 33
  • 46
4

Let say your html code is :

<input type="text" id="txtMyText" />

then the jquery should be :

$('#txtMyText').keyup(function() {
  this.value = this.value.toUpperCase();
});
Naftali
  • 144,921
  • 39
  • 244
  • 303
Ariful Islam
  • 7,639
  • 7
  • 36
  • 54
  • And it you want to retain the input position add the line `var inputPosition = this.selectionStart;` before the value assignment and then add `this.selectionStart = this.selectionEnd = inputPosition;` after it. – Christopher King Jan 29 '16 at 14:16
3

As placeholder text is becoming more commonly supported, this update may be relevant.

My issue with the other answers is that applying the text-transform: uppercase css would also uppercase the placeholder text, which we didn't want.

To work around this, it was a little tricky but worth the net effect.

  1. Create the text-uppercase class.

    .text-uppercase {
        text-transform:uppercase;
    }
    
  2. Bind to the keydown event.

    Binding to the keydown event was important to get the class to be added before the character was visible. Binding to keypress or keyup left the brief flicker of the lowercase letter.

    $('input').on('keydown', function(e)
    {
        // Visually Friendly Auto-Uppercase
        var $this = $(this);
    
        // 1. Length of 1, hitting backspace, remove class.
        if ($this.val().length == 1 && e.which == 8)
        {
            $this.removeClass('text-uppercase');
        }
    
        // 2. Length of 0, hitting character, add class.
        if ($this.val().length == 0 && e.which >= 65 && e.which <= 90)
        {
            $this.addClass('text-uppercase');
        }
    });
    
  3. Transform to uppercase when submitting to server.

    var myValue = this.value.toUpperCase();
    

YMMV, you may find that cutting text or deleting text with the delete key may not remove the class. You can modify the keydown to also take the delete character into account.

Also, if you only have one character, and your current cursor position is in position 0, hitting backspace will remove the text-transform class, but since the cursor position is in position 0, it doesn't delete the single character.

This would require a bit more work to also check the current character position and determine if the delete or backspace key will actually delete the single remaining character.

Although it was worth the extra effort to make this seemless and visually appealing for our most common use case, going beyond this wasn't necessary. :)

Dustin Graham
  • 2,054
  • 1
  • 20
  • 24
3

Solution 1 (Elegant approach with great user experience)

HTML

<input id="inputID" class="uppercase" name="inputName" value="" />

CSS

.uppercase{
    text-transform: uppercase;
}

JS

$('#inputID').on('blur', function(){
    this.value = this.value.toUpperCase();
});

By using CSS text-transform: uppercase; you'll eliminate the animation of lower to uppercase as the user types into the field.

Use blur event to handle converting to uppercase. This happens behind the scene as CSS took care of the user's visually appealing masking.

Solution 2 (Great, but less elegant)

If you insist on using keyup, here it is...

$('#inputID').on('keyup', function(){
    var caretPos = this.selectionStart;
    this.value = this.value.toUpperCase();
    this.setSelectionRange(caretPos, caretPos);
});

User would notice the animation of lowercase to uppercase as they type into the field. It gets the job done.

Solution 3 (Just get the job done)

$('#inputID').on('keyup', function(){
    this.value = this.value.toUpperCase();
});

This method is most commonly suggested but I do not recommend.

The downside of this solution is you'll be annoying the user as the cursor's caret position keeps jumping to the end of the text after every key input. Unless you know your users will never encounter typos or they will always clear the text and retype every single time, this method works.

Bill L.
  • 91
  • 5
2

jQuery:

var inputs = $('#foo');

inputs.each(function(){
          this.style.textTransform = 'uppercase';
       })
       .keyup(function(){
          this.value = this.value.toUpperCase();
       });

  1. Set the input's style to capitals (so user doesn't see the change)
  2. Automatically adjust the value (so the user doesn't have to hold shift or use caps lock)
Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
0

This worked for me

jQuery(document).ready(function(){ 
        jQuery('input').keyup(function() {
            this.value = this.value.toLocaleUpperCase();
        });
        jQuery('textarea').keyup(function() {
            this.value = this.value.toLocaleUpperCase();
        });
 });
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
prem
  • 843
  • 1
  • 8
  • 21
0

I success use this code to change uppercase

$(document).ready(function(){
$('#kode').keyup(function()
{
    $(this).val($(this).val().toUpperCase());
});
});
</script>

in your html tag bootstraps

<div class="form-group">
                            <label class="control-label col-md-3">Kode</label>
                            <div class="col-md-9 col-sm-9 col-xs-12">
                                <input name="kode" placeholder="Kode matakul" id="kode" class="form-control col-md-7 col-xs-12" type="text" required="required" maxlength="15">
                                <span class="fa fa-user form-control-feedback right" aria-hidden="true"></span>
                            </div>
                        </div>
0

There are many ways to get this done as shown above answers. CSS: Directly using style="text-transform:uppercase;" in HTML tag would be fastest way without even showing the original character pressed. However, this will make everything uppercase in that tag. For instance, camel case place holder in input field will be uppercase as well.

Next cleaner version would be JS in HTML tag;

<input type="text" onkeyup="this.value = this.value.toLowerCase();">

OR

<input type="text" onkeyup="$(this).val($(this).val().toLowerCase());">
esenkaya
  • 99
  • 4
-1

I work with telerik radcontrols that's why I Find a control, but you can find control directly like: $('#IdExample').css({

this code works for me, I hope help you and sorry my english.

Code Javascript:

<script type="javascript"> 
function changeToUpperCase(event, obj) {
var txtDescripcion = $("#%=RadPanelBar1.Items(0).Items(0).FindControl("txtDescripcion").ClientID%>");
             txtDescripcion.css({
                 "text-transform": "uppercase"
             });
} </script>

Code ASP.NET

<asp:TextBox ID="txtDescripcion" runat="server" MaxLength="80" Width="500px"                                                             onkeypress="return changeToUpperCase(event,this)"></asp:TextBox>
nenita
  • 11
  • 1
-1

Make sure that the field has this attribute in its html.

ClientIDMode="Static"

and then use this in your script:

$("#NameOfYourTextBox").change(function () {
                 $(this).val($(this).val().toUpperCase());
             });
sebastian
  • 111
  • 1
  • 5
  • What? What does `ClientIDMode="Static"` do? – Naftali Apr 08 '16 at 14:06
  • Well, first of all it is a feature that is available in .net 4.0 and above. When multiple(more than one) instance of a control is rendered. The value that is incremented is inserted in front of the ID. and it is separated by and underscore character "_". i.e. Mainbody_Panel_Current Panel. Therefore if you are trying to call an ID of a particular item using jquery i.e. #NameOfYourTextBox it wont work because it has incremented values in front of the id. – sebastian Apr 08 '16 at 14:27
  • 2
    This question has nothing to do with `.NET` – Naftali Apr 08 '16 at 14:27
  • The reason i have mentioned ClientID="Static" is because I have used the #"NameOfYourTextBox" in the answer i have provided and i know this possibly wouldn't work if the ClientIDMode value wasn't set to Static. Because JQuery is looking for the exact name of the control. – sebastian Apr 08 '16 at 14:29
  • 1
    I thought I've seen a .NET tag. My bad. – sebastian Apr 08 '16 at 14:31
  • Nope. No [tag:.net] here. – Naftali Apr 08 '16 at 14:32