How to change the input to uppercase as it is being typed

前端 未结 5 1415
盖世英雄少女心
盖世英雄少女心 2020-12-16 03:08

I am using onkeyup=\"this.value=this.value.toUpperCase();\"to change input text value in uppercase. This is working but my need is to change a single letter in

相关标签:
5条回答
  • 2020-12-16 03:47

    A sample could be:

    <p:inputText id="nombres" 
                 value="#{userController.user.nombres}" 
                 style="text-transform: uppercase" />
    
    0 讨论(0)
  • 2020-12-16 03:49

    How about CSS:

    input.upper { text-transform: uppercase; }
    

    Remark: This will still send the value to the server as typed by the user and not uppercased.

    0 讨论(0)
  • 2020-12-16 03:53

    bootstrap 3 has transformation classes which transform text in components with text capitalisation classes.

    <p class="text-lowercase">Lowercased text.</p>
    <p class="text-uppercase">Uppercased text.</p>
    <p class="text-capitalize">Capitalized text.</p>
    
    0 讨论(0)
  • 2020-12-16 03:57

    You could check what key was pressed in the onkeyup and only process for keys a-z. Should be pretty easy since the keycodes are numeric and you could just check if the keycode is between the keycode for a (65) and z (90).

    0 讨论(0)
  • 2020-12-16 04:04

    Like kristoffer says, probably best to use the style, and convert serverside. There's also a jQuery plugin to force uppercase: http://plugins.jquery.com/plugin-tags/uppercase

    <html>
        <head>
            <style>
                input.upc { text-transform: uppercase; }
            </style>
            <script>
                // thanks 2 'm2pc' : http://www.webdeveloper.com/forum/showpost.php?s=9f258cba84d461026bb9ed478e86776d&p=423545&postcount=3
                function doGetCaretPosition (oField) {
                    var iCaretPos = 0;
                    if (document.selection) // IE Support
                        {
                        oField.focus ();
                        var oSel = document.selection.createRange ();
                        // Move selection start to 0 position
                        oSel.moveStart ('character', -oField.value.length);
                        // The caret position is selection length
                        iCaretPos = oSel.text.length;
                        }
                    else
                        if (oField.selectionStart || oField.selectionStart == '0') // Firefox support
                            iCaretPos = oField.selectionStart;
                    return (iCaretPos);
                    }
                function doSetCaretPosition (oField, iCaretPos)
                    {
                    if (document.selection) // IE Support
                        {
                        oField.focus ();
                        var oSel = document.selection.createRange ();
                        oSel.moveStart ('character', -oField.value.length);
                        oSel.moveStart ('character', iCaretPos);
                        oSel.moveEnd ('character', 0);
                        oSel.select ();
                        }
                    else
                        if (oField.selectionStart || oField.selectionStart == '0') // Firefox support
                            {
                            oField.selectionStart = iCaretPos;
                            oField.selectionEnd = iCaretPos;
                            oField.focus ();
                            }
                    }
                function forceupper(o)
                    {
                    var x = doGetCaretPosition(o);
                    o.value=o.value.toUpperCase();
                    doSetCaretPosition(o,x);
                    }
            </script>
        </head>
        <body>
            <form method="get" target="#">
                Fld 1 : browser shows upper-case, form submits mixed-case<br>
                <input name="f1" id="idf1" class="upc" type="text" value="X"><br>
                Fld 2 : javascript updates text to uppercase, form submits uppercase<br>
                <input name="f2" id="idf2" class="js" type="text" value="Y" onkeyup="forceupper(this);"><br>
                <input type="submit" value="send">
            </form>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题