Only part of the value of an input

Asked

Viewed 39 times

0

Good morning,

I’m willing to take just a fraction of the value of a input.

Ex:

<input type="text" value="João Paulo Silva" id="nome" />

var nome = $("#nome").text();

And bring forth the following result:

joao.silva

If anyone knows, I’d really appreciate it.

  • Have you tried var nome = $("#nome").val().split(" "); alert(nome[0]); alert(nome[nome.length-1]); ?

1 answer

1


whereas his input be it:

<input type="text" value="João Paulo Silva" id="nome" />

You can return the name in the format specified in the question using the function in the code below.

The function will discard the middle names (as well as parts of the name, such as "of", "of", "of", "of", "of" and "and", ex.: John Paul of Silva), take only the first and last names and concatenate a "dot." in the middle of the two, in addition to converting everything to lowercase and eliminating possible accents:

function nomeAbrev(){

   var nome = $("#nome").val().toLowerCase();

   var reg_ex = "/ de | do | dos | da | das | e /i";
   
   nome = nome.replace(reg_ex," ").split(" ");
   nome = nome.shift()+"."+nome.pop();

   var acentos = "ãáàâäéèêëíìîïõóòôöúùûüç";
   var sem_acentos = "aaaaaeeeeiiiiooooouuuuc";

   for(var x=0; x<nome.length; x++){
      var str_pos = acentos.indexOf(nome.substr(x,1));
      if(str_pos != -1){
         nome = nome.replace(acentos.charAt(str_pos),sem_acentos.charAt(str_pos));
      }
   }

   console.log(nome);

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="João Paulo Silva" id="nome" />
<br />
<input type="button" value="Clique para Converter" onclick="nomeAbrev()" />

  • It worked, just what I needed. Thank you very much

Browser other questions tagged

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