First letter uppercase, the rest lower case

This question already has an answer here:

  • How do I make the first letter of a string uppercase in JavaScript? 69 answers

  • This is a simple script that will allow you to do this.

    <input type="text" id="name" onblur="capitalize(this.id)" value="" autofocus>
    
    <script>
        function capitalize(id)
        {
            var text = document.getElementById(id).value; //get the text
            var firstL = text.slice(0,1).toUpperCase(); //get the first character and make it Uppercase
            var rest = text.slice(1).toLowerCase(); //get the rest of the string and make it Lowercase
            var text = firstL.concat(rest); //create the new text store it
            document.getElementById(id).value = text; //set the value of the field
        }
    </script>
    

    The onblur event could be changed with a different event such as onkeyup for more immediate results.

    链接地址: http://www.djcxy.com/p/17868.html

    上一篇: 不能使用'toUpperCase'(JS)

    下一篇: 首字母大写,其余小写