开发者

Convert lowercase letter to upper case in javascript

开发者 https://www.devze.com 2022-12-21 18:38 出处:网络
I have a code that will convert lower case letters to uppercase but it works only with IE and not in Crome or Firefox.

I have a code that will convert lower case letters to uppercase but it works only with IE and not in Crome or Firefox.

function ChangeToUpper()
  {         
            key = window.event.which || window.event.keyCode;

            if ((key > 0x60) && (key < 0x7B))
            window.event.keyCode = key-0x20;
  }



<asp:TextBox ID="txtJobId" runat="server" MaxLength="10" onKeypress="ChangeToUpper();"></asp:TextBox>

Even I tried with

document.getElementById("txtJobId").value=document.getElementById("txtJobId").value.toUpperCase(); 

onBlur event of the textbox

开发者_如何转开发

What should I do to make it work in all browsers?

Thanks


<script type="text/javascript">
    function ChangeCase(elem)
    {
        elem.value = elem.value.toUpperCase();
    }
</script>
<input onblur="ChangeCase(this);" type="text" id="txt1" />

separate javascript from your HTML

window.onload = function(){
        var textBx = document.getElementById ( "txt1" );

        textBx.onblur = function() {
            this.value = this.value.toUpperCase();
        };
    };

<asp:TextBox ID="txt1" runat="server"></asp:TextBox>

If the textbox is inside a naming container then use something like this

var textBx = document.getElementById ("<%= txt1.ClientID %>");

            textBx.onblur = function() {
                this.value = this.value.toUpperCase();
            };)


Have you tried .toUpperCase()?

Links:

  • Formatting text using JavaScript
  • JAVASCRIPT toUpperCase() METHOD


If you don't want to make an explicit JavaScript function, here you can do it in just one line:

Convert to lower and upper case respectively:

<asp:TextBox ID="txt1" onblur='this.value = this.value.toLowerCase();'></asp:TextBox>
<asp:TextBox ID="txt1" onblur='this.value = this.value.toUpperCase();'></asp:TextBox>


You can simply use CSS and do text-transform:uppercase and on submit you run toUppercase(). Or you just submit as mixed and you capitalize letters on server side :)


I would say the easiest way is..

<input id="yourid" style="**text-transform: uppercase**" type="text" />


Have you tried this?

var myString = "this is a String";
alert(myString.toUpperCase()); // "THIS IS A STRING"
alert(myString.toLowerCase()); // "this is a string"

Thanks... Hope you like it.

0

精彩评论

暂无评论...
验证码 换一张
取 消