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()" />
Have you tried
var nome = $("#nome").val().split(" "); alert(nome[0]); alert(nome[nome.length-1]);
?– Valdeir Psr