Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I change to uppercase w/ mouse paste too?

    Hi everyone, I've been trying to amend this so that when I paste text in with a mouse it changes to uppercase automatically. Does anyone know how this can be done?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function upperCase(x)
    {
    var y=document.getElementById(x).value;
    document.getElementById(x).value=y.toUpperCase();
    }
    </script>
    </head>
    
    <body>
    Enter your name: <input type="text" id="fname" onchange="upperCase(this.id)" />
    </body>
    
    </html>
    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    onkeyup = "upperCase(this.id)" onblur="upperCase(this.id)"
    But all upper-case does not look too good. Why not format the name with the usual capitals and lower case?

    Code:
    var str1 = "jEAn-paul o'flaNAGan-macDONald"
    str1 = str1.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    alert(str1); // Jean-Paul O'Flanagan-Macdonald


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Thinius, you're going around by passing this.id then in the function you get the object reference of that id when you already have the object reference and can simply pass this.

    Code:
    function upperCase(obj)
    {
        obj.value = obj.toUpperCase();
    }
    ...
    <input type="text" id="fname" onkeyup="upperCase(this);" onblur="upperCase(this);" />

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    // never mind, answered
    Last edited by mrhoo; 11-14-2010 at 04:44 PM.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Actually, you can capitalize it even without javascript by using CSS text-transform property.

    Code:
    Enter your name: <input type="text" id="fname" style="text-transform:uppercase;" />
    This will uppercase the input on the display only. Internally, it's still not uppercased, so if you want to also make it uppercase when you process it on client-side or server-side, you should uppercase it accordingly.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by glenngv View Post
    Actually, you can capitalize it even without javascript by using CSS text-transform property.

    Code:
    Enter your name: <input type="text" id="fname" style="text-transform:uppercase;" />
    This will uppercase the input on the display only. Internally, it's still not uppercased, so if you want to also make it uppercase when you process it on client-side or server-side, you should uppercase it accordingly.
    With respect, that makes it useless. In any case, as I say, all upper-case does not look too good (SHOUTING!). Why not format the name with the usual capitals and lower case?

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    We don't know why the OP wants it all uppercase. He/she may have a good reason to do that. In any case, I still prefer the CSS solution as I think the main reason why you want a text to be uppercased is for display purposes. I don't think it really matters if what is stored on the backend is uppercased or not.

    And to capitalize only the first letter, you just need to set text-transform to capitalize.
    Code:
    <input type="text" id="fname" style="text-transform:capitalize;" />

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by glenngv View Post
    We don't know why the OP wants it all uppercase. He/she may have a good reason to do that. In any case, I still prefer the CSS solution as I think the main reason why you want a text to be uppercased is for display purposes. I don't think it really matters if what is stored on the backend is uppercased or not.

    And to capitalize only the first letter, you just need to set text-transform to capitalize.
    Code:
    <input type="text" id="fname" style="text-transform:capitalize;" />
    Does not work with O'Flanagan (O'flanagan).

    For the rest, we will have to agree to disagree.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •