-3

I need strings to be transformed, having the first letter uppercase, and all the rest lowercase, to store in my database.

For example, the user inputs

name="john";

And I need it to be transformed into

name="John"; 
Ema Black
  • 63
  • 1
  • 12
  • 1
    This has been asked already, no need to provide duplicated Q&A. – nicael Jun 18 '16 at 18:14
  • Not an exact duplicate, as OP asks for the remainder to be lowercased - the linked question is about uppercasing the first character only. Obviously both are simple questions, but not duplicates. This question here needs clarification, though. – le_m Jun 18 '16 at 22:05
  • @MrUber Please clarify your question: What do you mean with "to store in my database"? Are you looking for "live" upper-lowercasing on the client side (which can always be circumvented) or server-side? – le_m Jun 18 '16 at 22:07
  • @le_m yes, but this is a Q&A question, i.e. I have already created the script needed! – Ema Black Jun 19 '16 at 11:29

1 Answers1

0

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.

Ema Black
  • 63
  • 1
  • 12