First letter uppercase, the rest lower case
This question already has an answer here:
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.
下一篇: 首字母大写,其余小写