Regular Expression - Name + Space + Last Name

2

Hello, I'm looking for a regular expression to validate name and surname mandatory. But it should accept accents and a specific case that would be:

Igor D'Sant

    
asked by anonymous 11.12.2017 / 19:35

3 answers

1

Regex:

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

This regex will only accept:

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

  • the ´

  • 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><inputtype="text" id="nome" />
<br>
<input type="button" value="Verificar" onclick="verifica()" />
    
12.12.2017 / 01:54
1

I believe this simple regular expression solves your problem:

/\w+\s+\w/

Example use in javascript, 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.

    
11.12.2017 / 19:55
0
/^[A-zÀ-ú '´]+$/
  • A-zÀ-ú recognizes all uppercase, lowercase and accented letters regardless of case
  • % with more space and one of two forms ( [A-zÀ-ú '´] ) or ( ' ) of single quotes allowed
  

If you only want to recognize one of the quotes, just remove the unwanted 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>
    
11.12.2017 / 22:01