Regular Expression - Name + Space + Surname

Asked

Viewed 3,777 times

2

Hello ,I’m looking for a regular expression to validate name and last name requirement. But she must accept accents and a specific case that would be:

Igor D Sant

3 answers

1


Regex:

^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$

This regex will accept only:

  • letters (case insensitive) with accents (\u00C0-\u017F) or without accents

  • the symbol ´

  • string with at least 1 space in the middle

Will not accept:

  • special characters

  • numbers

  • underscore (underlined) _

Testing:

nome1 = "Igor D´Sant"; // normal
nome2 = "IgorD´Sant"; // sem espaço
nome3 = "Igor2 D´Sant"; // com número
nome4 = "Igor D´Sant teste"; // mais de 2 nomes
nome5 = "Igor D´Sánt"; // com acento
nome6 = "Igor D´S-ant"; // com caractere especial
nome7 = "Ig-or D´Sant"; // com caractere especial
nome8 = "Igór D´Sant"; // com acento
nome9 = "Igór_ D´Sant"; // com underscore
nome10 = "Igór"; // apenas 1 nome

if(nome1.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome1.trim().split(' ').length == 2 ){
   console.log(nome1+" = ok");
}else{
   console.log(nome1+" = erro");
}

if(nome2.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome2.trim().split(' ').length == 2 ){
   console.log(nome2+" = ok");
}else{
   console.log(nome2+" = erro");
}

if(nome3.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome3.trim().split(' ').length == 2 ){
   console.log(nome3+" = ok");
}else{
   console.log(nome3+" = erro");
}

if(nome4.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome4.trim().split(' ').length == 2 ){
   console.log(nome4+" = ok");
}else{
   console.log(nome4+" = erro");
}

if(nome5.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome5.trim().split(' ').length == 2 ){
   console.log(nome5+" = ok");
}else{
   console.log(nome5+" = erro");
}

if(nome6.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome6.trim().split(' ').length == 2 ){
   console.log(nome6+" = ok");
}else{
   console.log(nome6+" = erro");
}

if(nome7.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome7.trim().split(' ').length == 2 ){
   console.log(nome7+" = ok");
}else{
   console.log(nome7+" = erro");
}

if(nome8.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome8.trim().split(' ').length == 2 ){
   console.log(nome8+" = ok");
}else{
   console.log(nome8+" = erro");
}

if(nome9.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome8.trim().split(' ').length == 2 ){
   console.log(nome9+" = ok");
}else{
   console.log(nome9+" = erro");
}

if(nome10.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome8.trim().split(' ').length == 2 ){
   console.log(nome109+" = ok");
}else{
   console.log(nome10+" = erro");
}

Test inserting name in field:

function verifica(){
   nome = $("#nome").val();
   
   if(nome.match(/^[a-zA-Z\u00C0-\u017F´]+\s+[a-zA-Z\u00C0-\u017F´]{0,}$/) && nome.trim().split(' ').length == 2){
       alert("nome válido");
   }else{
       alert("nome INVÁLIDO");
   }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="nome" />
<br>
<input type="button" value="Verificar" onclick="verifica()" />

  • If I want to add more name as the expression would be example: Igor Augusto Siqueira or Igor Santos Siqueira or add another space and string!

  • Another example would be that name taken from our base : THERESA LILIANNE DE SA BARRETO DIAZ ALBUQUERQUE SAMPAIO he would have to go through validation

  • These lengthy cases may occur, but need not be mandatory, but need to go through validation!

  • With this expression that I put together here I was able to release from 2 up to 60 characters, but I can’t accept Igor D Sant for example : /(?= .{2,60}$) [A-ZÀÁÂÈÈÈÈÉÊÊÒÓÒÙÚÇ][a-zàâáâèéêêéíóôùúç]+(?: )*$/)

  • Got the next one so far : $.validator.addMethod(&#xA; "NomeCompleto",&#xA; function(value, element, regexp) {&#xA; &#xA; if(value.match(/(?=^.{2,60}$)^[A-ZÀÁÂĖÈÉÊÌÍÒÓÔÕÙÚÛÇ´][a-zàáâãèéêìíóôõùúç´]+(?:[ ](?:das?|dos?|de|e|[A-Z][a-z]+))*$/)){&#xA; return true&#xA; }else{&#xA; return false&#xA;}&#xA; },&#xA; "Campo inválido."&#xA;);

  • @Igorsiqueira veja este fiddle https://jsfiddle.net/qxwx05kf/

  • @Igorsiqueira fiddle will validate any name from 2 words.

  • Perfect ! Gave straight !

Show 3 more comments

1

I believe this simple regular expression will solve your problem:

/\w+\s+\w/

Example of javascript usage, for example:

/\w+\s+\w/.test('Igor D´Sant'); // retorna true
/\w+\s+\w/.test('Igor'); // retorna false
/\w+\s+\w/.test('Fulano de Tal'); //retorna true

Explanation:

\w+ takes any typed word sentence.

\s+ takes any space found.

0

/^[A-zÀ-ú '´]+$/
  • A-zÀ-ú recognizes all capital letters, minuscules and accents independent of the case
  • [A-zÀ-ú '´] more space and one of the two forms (') or (´) single quote permitted

If you want to recognize only one of the quotation marks, just remove the unwanted one from the regex

      var re = /^[A-zÀ-ú '´]+$/ 

      function testInfo(nomeInput){  
        var OK = re.exec(nomeInput.value);  
        if (!OK)  
          console.log(document.getElementById("nome").value + " não é um nome válido!");  
        else
          console.log("Seu nome " + document.getElementById("nome").value + " é válido");  
      } 
<input id="nome" value="Igor D´Sant">
<button onclick="testInfo(document.getElementById('nome'));">Checar</button>

Browser other questions tagged

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