11

I used following codes to convert to upper case while typing.

      $(".input_capital").live('keypress', function(e)
      {
        var defaultStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var checkstr = $(this).val();
        var str1 = '';
        for (i = 0; i < checkstr.length; i++)
        {
            var ch = checkstr.charCodeAt(i);
             if (ch>=97 && ch<=122){
                str1 += defaultStr.charAt(ch-97);
             }else{
                str1 += checkstr.charAt(i);
              }
        }
        $(this).focus();
        $(this).val(str1);
     });

And the following code

  $(".input_capital").live('keypress', function(e)
  {
       $(this).val($(this).val().toUpperCase());
  });

all these above code is working fine. But for user able to view lower cases for some time. After that only its converting to upper case.

I tried with 'text-transform: uppercase' in css. But it is not working in Samsung tab with Android Os. Please anybody help me to achieve this by script.

Anupam
  • 7,966
  • 3
  • 41
  • 63
Ramaraj Karuppusamy
  • 2,350
  • 5
  • 24
  • 35

9 Answers9

26

Note: This answer is without delay..if you like to avoid Jquery

Try

<input type="text" style="text-transform: uppercase">

Demo - JSFiddle

Some Java Guy
  • 4,992
  • 19
  • 71
  • 108
23

you can try:

$(".input_capital").on('keydown', function(evt) {
  $(this).val(function (_, val) {
    return val + String.fromCharCode(evt.which).toUpperCase();
  });

  return false;
});​

http://jsfiddle.net/5gLyX/

It has some flaws, but I think the idea is clear, and can be build upon.


Better version, though mind the input event which is not supported in IE<9.

$('.input_capital').on('input', function(evt) {
  $(this).val(function(_, val) {
    return val.toUpperCase();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class='input_capital' rows='8'></textarea>
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Yoshi
  • 54,081
  • 14
  • 89
  • 103
5

if you don't care ie<9 , listen the "input" event

$(".input_capital").bind("input", function(e){
   this.value = this.value.toUpperCase();
});

and for IE<9, use "keyup" instead

demo : http://jsfiddle.net/MupXZ/6/

guirong.cao
  • 96
  • 1
  • 6
2

What about using keyup instead of keypress.

$(".input_capital").live('keyup', function(e)
  {
       $(this).val($(this).val().toUpperCase());
  });

So that the last letter is changed as soon as the key is released.

Like this : http://jsfiddle.net/mNVXK/

Chris Charles
  • 4,406
  • 17
  • 31
1

Why you just not add this line

$(this).val($(this).val().toUpperCase());

before $(this).focus();? And use on() instead of live()

coolguy
  • 7,866
  • 9
  • 45
  • 71
Morpheus
  • 8,829
  • 13
  • 51
  • 77
1

To extend @Yoshi's answer

This extension takes care of:

Backspace: 8
Tab: 9
Enter: 13
Arrow keys: 37,38,39,40

$(".input_capital").bind('keydown', function(event) {
    if ( $.inArray( event.keyCode, [8,9,13,37,38,39,40]) === -1 ) {
        $(this).val(function(i, val) {
            return val + String.fromCharCode(event.keyCode).toUpperCase();
        });
        return false;
    }
});

Using bind cause OP is using jQuery 1.5.1

Community
  • 1
  • 1
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0

Try:

$(".input_capital").live('keypress', function(e) {
    setInterval("$(this).val($(this).val().toUpperCase())",100);      
});
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
atredis
  • 372
  • 2
  • 10
0

for those using later versions of jquery replace live with on. eg.

 $(".input_capital").live('keyup', function(e)  {
   $(this).val($(this).val().toUpperCase());
});

is now

$(".input_capital").on('keyup', function(e)  {
   $(this).val($(this).val().toUpperCase());
});
macm
  • 544
  • 7
  • 21
0

js

$('.input_capital').on('blur', function(evt) {
  $(this).val(function (_, val) {
    return val.toUpperCase();
  });
});

html

<input type="text" class="input_capital" style="text-transform: uppercase" />

Follow this: http://jsfiddle.net/5gLyX/389/