Turn an entry’s uppercase characters into lowercase characters

Asked

Viewed 822 times

1

I have this simple form and a jquery code that automatically fills the email field with the first name of the 'name' field and adds '@dominio.com.br' to form an email address. How do I also make it turn the uppercase characters of the 'name' field into lowercase characters but only in the 'email field'?

jquery:

$(window).load(function(){
  $("input[name=nome]").change(function () {
      var nome = $(this).val();
      var pEspaco = nome.indexOf(' ');
      var nomeFinal = nome;
      if (pEspaco != -1) {
          nomeFinal = nome.substr(0, pEspaco);
      }
      $("input[name=email]").val(nomeFinal + "@dominio.com.br");
  });
}); 

html:

        <form name="campos" method="post" action="<?=$PHP_SELF?>"> 
            <label>
                <span>Nome:</span>
                <input type="text" name="nome"  placeholder="" /><br>
            </label>
            <label>
                <span>Cargo:</span>
                <input type="text" name="cargo" placeholder="" /><br>
            </label>
            <label>
                <span>Celular:</span>
                <input type="text" name="celular" placeholder="Exemplo: 44 9876 9876" /><br>
            </label>
            <label>
                <span>e-mail:</span>
                <input type="text" name="email" placeholder="" /><br>
            </label>
            <input type="hidden" name="acao" value="enviar" />
            <button type="submit" class="envio" title="Enviar mensagem"></button> 
        </form>

1 answer

1

You can use the method .toLowerCase() which is native to Javascript.

So I would be for example:

$("input[name=email]").val(nomeFinal.toLowerCase() + "@dominio.com.br");

You can simplify it a little bit and use it like this:

$(window).load(function () {
    $("input[name=nome]").change(function () {
        var partes = this.value.split(' ');
        var email = partes[0];
        $("input[name=email]").val(email.toLowerCase() + "@dominio.com.br");
    });
});

If you want the last name, you can use var email = partes[partes.length - 1];

Example: http://jsfiddle.net/Stf6J/1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.