Regex for name checking

I would like to create a regular expression that validates a person’s name. They must be allowed:

  • Letters (upper and lower case)
  • -
  • spaces

It is quite simple to create a regular expression. The problem is that some people also use special characters in their names. For example, suppose a user named gûnther or François. There are many characters, such as û and ç, and it is difficult to list all of these.

Is there an easy way to check the correct names of people?

+4
source share
6 answers

Is there an easy way to check the correct names of people?

This has been discussed several times. I am pretty sure that the only thing people can agree on is that in order to exist, a name cannot be an empty string, thus:

 ^.+$ 

(Yes, I know that this is probably not what the OP is looking for. I just summarize Q & As earlier.)

+7
source

/^\pL[\pL '-]*\z/ should do the trick

+7
source

The short answer is no, there is no easy way. You have raised the biggest problem. There are so many special cases of accents and extra things hanging in letters that it will be a mess to deal with. Also, an expression broken down by something like this

 ^[CAPITAL_LETERS][ALL_LETERS_AND_SYMBOLS]*$ 

This is not so useful because “Abcd” matches this, and you cannot find out if someone is typing the information incorrectly in the field, or if it was a crazy Hollywood parent who actually called his child this or that sandwich or umbrella.

+1
source
 ^.+$ 

@Jensgram's answer was checked, but this regular expression only accepts all strings, so it does not solve the problem, because the string must be a name, in this case it can be anything.

 ^([AZ]{1})[az]+$ 

My regular expression only accepts a string where the first char is uppercase and the following characters are lowercase letters. Also, while looking at other answers, this seems to be the shortest regular expression, as well as the simplest.

+1
source

I do not know exactly what you are trying to do (confirm the username?), But basically I would keep it simple - I could not verify whether the text contains numbers. And even that is probably pretty shaky.

0
source

I had the same problem. At first I came up with something like

  preg_match("/^[a-zA-Z]{1,}([\s-]*[a-zA-Z\s\'-]*)$/", $name)) 

but then I realized that the UTF-8 characters of countries such as Sweden, China, etc., for example, Õ å, will not be allowed, which is important for my site, since this is an international site, and they do not want to force users to not enter your real name.

Although this might be an easier solution instead of trying to figure out how to resolve names like O'Malley and Brooks-Schneider and Õsmar (do it one :) to catch characters that you don't want them to enter more quickly. For me, it was basically avoiding typing xss JS code. Therefore, I use the following regular expression to filter out all characters that may be harmful.

  preg_match("/[ ~!@ #\$%\^&\*\(\)=\+\|\[\]\{\};\\:\",\.\<\>\?\/]+/", $name) 

Thus, they can enter any name they want, except for characters that really are not part of any name. Hope this can be helpful.

0
source

Source: https://habr.com/ru/post/1341138/


All Articles