How to validate with regex a string containing only letters, blanks and letters with an accent?

23

I want to validate an input text for full name in order to accept only letters, blanks and accented letters.

  

Valid Formats:
  Leandro Moreira
  leandro moreira
  Kézia Maria
  kezia maria
  Cabaço da silva
  Cabaço da Silva

This regex can not accept special characters outside letters with an accent.

    
asked by anonymous 14.05.2014 / 00:05

5 answers

35

If the target is just the accents common in Portuguese, it is easy to list them one by one. I would make a regex with the following blocks:

  • A-Za-z case not accented
  • áàâãéèêíïóôõöúçñ : accented vowels of Portuguese, cedilha and a few of lambuja, lowercase
  • ÁÀÂÃÉÈÊÍÏÓÔÕÖÚÇÑ : accented vowels of Portuguese, cedilla and others of lambuja, capital letters
  • spaces

Therefore:

/^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]+$/

Or, leaving the lowercase / uppercase distinction for the implementation:

/^[a-záàâãéèêíïóôõöúçñ ]+$/i

Demo (of this second option)

    
14.05.2014 / 00:11
12

I liked the bfavaretto solution more, but I'll leave a shorter alternative here:

var reg = /[a-zA-Z\u00C0-\u00FF ]+/i

Explanation: this is a match of the characters 'a' through 'z', 'A' through 'Z', and finally, all Unicode characters from 'À' to 'ÿ', so I think should include all accented characters.

The full list of Latin characters in Unicode can be seen here: link

The complete list contains about 128 characters, of which only one part is used. The expression I used includes some strange and unlikely ones, like the Æ and at least two mathematical signs. You may want to use a more accurate expression such as bfavaretto, or use narrower Unicode strings.

    
14.05.2014 / 00:16
4

If the surname is mandatory and there is only one field to validate, you can use the following regex:

/\b[A-Za-zÀ-ú][A-Za-zÀ-ú]+,?\s[A-Za-zÀ-ú][A-Za-zÀ-ú]{2,19}\b/gi

It only validates letters, accents, and white space. It does not accept numbers and each string (name) must be at least 3 characters long.

Demo: jsfiddle

    
15.11.2016 / 23:54
3

Completing bfavareto's response, when putting regular expression code for names, remember that we can have situations like Antônio D'Ávila. In these cases, also enter the apostrophe. And the space is interesting to leave to the machine, with the \ s:

/^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ'\s]+$/

    
18.06.2016 / 14:03
1
function removeSpecialCharSimple(strToReplace) {
strSChar = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ";
strNoSChars = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC";
var newStr = "";
for (var i = 0; i < strToReplace.length; i++) {
    if (strSChar.indexOf(strToReplace.charAt(i)) != -1) {
        newStr += strNoSChars.substr(strSChar.search(strToReplace.substr(i, 1)), 1);
    } else {
        newStr += strToReplace.substr(i, 1);
    }
}

return newStr.replace(/[^a-zA-Z 0-9]/g, '');
}
    
03.02.2015 / 23:06