How to check names in ASP.NET MVC so accents are allowed (é, á, ...)?

I need to check the fields of the form "name", which may contain characters with accents such as á, é, etc.

I tried to apply the regex in the following attribute, as pointed out in another SO question (apologies that I cannot find now), and so far it is being checked correctly for most characters that I don't want (i.e. *, ^ ,?) it also marks accented characters as invalid.

Is it because I check on the client side not the server?

Any advice would be appreciated.

[ValidateRegExp(@"^\w*$", "Invalid characters in surname")] 
+4
source share
4 answers

I will not give you a regular expression, because you really should not check people's names.
This is one of those that so easily make mistakes and offend your users .

What are the benefits of this anyway?
The rudest thing you can probably do is say something similar to "Invalid characters in last name . " My surname is Abramov, and the characters are completely valid, it's just that your system is not smart enough. The user interface should not blame me for your mistake.

If accepting such characters really destroys your database, at least respond to "We are very sorry, but our system does not accept anything other than English letters."

Take a look at this wonderful Patrick post:

Local programmers believe in names

  • People only have one full name.
  • People have one full name that they go through.
  • At the moment, people have exactly one canonical full name.
  • People have at the moment one full name that they go through.
  • People have exactly N names, for any value of N.
  • The names of people correspond to a certain space.
  • Names of people do not change.
  • Names of people change, but only with a certain enumerable set of events.
  • The names of the peoples are written in ASCII.
  • Names of people are recorded in any character set.
  • Names of people are displayed in Unicode code points.
  • Names of people are case sensitive.
  • Names of people are case insensitive.
  • People’s names sometimes have prefixes or suffixes, but you can safely ignore them.
  • Names of people do not contain numbers.
  • Names of people are not recorded in ALL CAPS.
  • Names of people are not written in all lowercase letters.
  • The names of people have an order for them. Selecting any ordering scheme will automatically lead to sequential ordering among all systems if both use the same ordering scheme for the same name.
  • Names of people and surnames, of necessity, are different.
  • People have surnames, surnames, or something else shared by people recognized by their relatives.
  • The names of peoples are unique throughout the world.
  • The names of peoples are almost globally unique.
  • Good, but true, people's names are quite diverse, so millions of people have the same name.
  • My system will never deal with names from China.
  • Or Japan.
  • Or Korea.
  • Or Ireland, the United Kingdom, the United States, Spain, Mexico, Brazil, Peru, Russia, Sweden, Botswana, South Africa, Trinidad, Haiti, France, or the Klingon Empire, all of which have the "strange" name of the public scheme.
  • That the Klingon Empire was a joke, right?
  • Mix your cultural relativism! People in my society at least agree to one generally accepted standard for names.
  • There is an algorithm that converts names and can be undone without loss. (Yes, yes, you can do this if your algorithm returns the input. You get a gold star.)
  • I can safely assume that this dictionary of bad words does not contain the names of the people in it.
  • Names of people are assigned at birth.
  • Well, maybe not at birth, but at least close to birth.
  • Good, good, for about one year of birth.
  • Five years?
  • Are you kidding me, right?
  • Two different systems containing data about the same person will use the same name for that person.
  • Two different data entry operators, taking into account the name of the person, will enter bitwise equivalent strings in any separate system if necessary, if the system is well developed.
  • The people whose names break my system are weird outbursts. They should have solid, acceptable names, such as 田中 太郎.
  • People have names.
+9
source
  • \p{L} or \p{Letter} = any Unicode character recognized as a letter.
  • \p{N} or \p{Number} = any Unicode character recognized as a number.

Other options: http://www.regular-expressions.info/unicode.html#prop

 [ValidateRegExp(@"^[\p{L}\p{N}]*$", "Disallowed characters in surname")] 
+3
source

Try this character set: [a-zA-ZÀ-ÿ0-9] . The third range will correspond to all accented characters.

 [ValidateRegExp(@"^[a-zA-ZÀ-ÿ0-9]*$", "Invalid characters in surname")] 
0
source

How to do without regex and use something like:

 s.All(c=>char.IsLetter(c)) 

You must also normalize the string first . This way you can deal with accented characters since the main character and accent are in different char s.

0
source

All Articles