With jQuery, how do I capitalize the first letter of a text field while the user is still editing that field?

后端 未结 21 2494
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 16:26

I\'m looking for an example of how to capitalize the first letter of a string being entered into a text field. Normally, this is done on the entire field with a function, r

21条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 16:47

    I used the code of @Spajus and wrote a more extended jQuery plugin.

    I wrote these four jQuery functions:

    • upperFirstAll() to capitalize ALL words in an inputfield
    • upperFirst() to capitalize only the FIRST word
    • upperCase() to convert the hole text to upper case
    • lowerCase() to convert the hole text to lower case

    You can use and chain them like any other jQuery function:
    $('#firstname').upperFirstAll()

    My complete jQuery plugin:

    (function ($) {
        $.fn.extend({
    
        // With every keystroke capitalize first letter of ALL words in the text
        upperFirstAll: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toLowerCase().replace(/^(.)|(\s|\-)(.)/g,
                function(c) {
                    return c.toUpperCase();
                }));
                box.setSelectionRange(start, end);
            });
            return this;
        },
    
        // With every keystroke capitalize first letter of the FIRST word in the text
        upperFirst: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toLowerCase().replace(/^(.)/g,
                function(c) {
                    return c.toUpperCase();
                }));
                box.setSelectionRange(start, end);
            });
            return this;
        },
    
        // Converts with every keystroke the hole text to lowercase
        lowerCase: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toLowerCase());
                box.setSelectionRange(start, end);
            });
            return this;
        },
    
        // Converts with every keystroke the hole text to uppercase
        upperCase: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toUpperCase());
                box.setSelectionRange(start, end);
            });
            return this;
        }
    
        });
    }(jQuery));
    

    Groetjes :)

提交回复
热议问题