jQuery - How to accept only letters and accents

Asked

Viewed 1,068 times

0

I need to do a validation. It’s as follows: The field can only accept normal names, like:

João Pedro, Mateus Rodrigues etc.

Names like Fabio123 or other special characters cannot be accepted. Someone can help me, I couldn’t do.

1 answer

1

Use regular expression: [A-Za-záààâéèíííïóõõöúçñALLLIRON-CONORMION ]

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>

<input type="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'));">

Browser other questions tagged

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