jQuery - Accepting all letters and accents

0

I need to do a validation. The following is: The field can only accept normal names, type:

João Pedro, Mateus Rodrigues, etc.

Names like Fabio123 or other special characters can not be accepted. Someone can help me, I could not do it.

    
asked by anonymous 01.04.2017 / 03:50

1 answer

1

Use regular expression: [A-Za-záàâãéèêïïôôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ]

JQuery keyup function

 jQuery('.nome').keyup(function () { 
   this.value = this.value.replace(/[^A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]/g,'');
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><inputtype="text" class="nome" value="" />

Javascript onclick event

    var re = /^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]+$/  
      function testInfo(nomeInput){  
        var OK = re.exec(nomeInput.value);  
        if (!OK)  
          window.alert(document.getElementById("nome").value + " não é um nome válido!");  
        else
          window.alert("Seu nome " + OK[0] + " é válido");  
      }
  
    
  <input id="nome">
      <button onclick="testInfo(document.getElementById('nome'));">Check</button>

Javascript onkeyup event

	function myFunction(nomeInput) {
    	var el = document.getElementById("nome");
    	var str = el.value;
    	var res = str.replace(/[^A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]/g, "");
   	 	el.value = res;
	}
    <input type="text" class="nome" id="nome" value="" onkeyup="myFunction(document.getElementById('nome'));">
    
01.04.2017 / 06:00