c# - 如何验证 ASP .NET MVC 中的名称以便允许使用重音符号(é、á、...)?

标签 c# regex asp.net-mvc

我需要验证表单“名称”字段,这些字段可能包含带重音符号的字符,例如 á、é 等。

我尝试按照另一个 SO 问题中的指示在以下属性中应用正则表达式(抱歉我现在找不到它)并且它正确验证了我不想要的大多数字符(即 *、^、 ?)它还将重音字符标记为无效。

这是因为我在客户端而不是服务器端进行验证吗?

如有任何建议,我们将不胜感激。

[ValidateRegExp(@"^\w*$", "Invalid characters in surname")]

最佳答案

我不会给你正则表达式,因为你真的不应该验证人名。
这是很容易出错的事情之一,offend your users .

这有什么好处呢?
您可能会做的最粗鲁的事情就是说类似于“姓氏中的无效字符”的话。我姓 Абрамов,字都对,就是你的系统不够智能。用户界面不应该因为的错误而责怪我。

如果接受这样的字符真的会破坏你的数据库,请至少回复“我们非常抱歉,我们的系统只接受英文字母。”

看看 Patrick 的这篇精彩帖子:

Falsehoods Programmers Believe about Names

  1. People have exactly one canonical full name.
  2. People have exactly one full name which they go by.
  3. People have, at this point in time, exactly one canonical full name.
  4. People have, at this point in time, one full name which they go by.
  5. People have exactly N names, for any value of N.
  6. People’s names fit within a certain defined amount of space.
  7. People’s names do not change.
  8. People’s names change, but only at a certain enumerated set of events.
  9. People’s names are written in ASCII.
  10. People’s names are written in any single character set.
  11. People’s names are all mapped in Unicode code points.
  12. People’s names are case sensitive.
  13. People’s names are case insensitive.
  14. People’s names sometimes have prefixes or suffixes, but you can safely ignore those.
  15. People’s names do not contain numbers.
  16. People’s names are not written in ALL CAPS.
  17. People’s names are not written in all lower case letters.
  18. People’s names have an order to them. Picking any ordering scheme will automatically result in consistent ordering among all systems, as long as both use the same ordering scheme for the same name.
  19. People’s first names and last names are, by necessity, different.
  20. People have last names, family names, or anything else which is shared by folks recognized as their relatives.
  21. People’s names are globally unique.
  22. People’s names are almost globally unique.
  23. Alright alright but surely people’s names are diverse enough such that no million people share the same name.
  24. My system will never have to deal with names from China.
  25. Or Japan.
  26. Or Korea.
  27. 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 “weird” naming schemes in common use.
  28. That Klingon Empire thing was a joke, right?
  29. Confound your cultural relativism! People in my society, at least, agree on one commonly accepted standard for names.
  30. There exists an algorithm which transforms names and can be reversed losslessly. (Yes, yes, you can do it if your algorithm returns the input. You get a gold star.)
  31. I can safely assume that this dictionary of bad words contains no people’s names in it.
  32. People’s names are assigned at birth.
  33. OK, maybe not at birth, but at least pretty close to birth.
  34. Alright, alright, within a year or so of birth.
  35. Five years?
  36. You’re kidding me, right?
  37. Two different systems containing data about the same person will use the same name for that person.
  38. Two different data entry operators, given a person’s name, will by necessity enter bitwise equivalent strings on any single system, if the system is well-designed.
  39. People whose names break my system are weird outliers. They should have had solid, acceptable names, like 田中太郎.
  40. People have names.

关于c# - 如何验证 ASP .NET MVC 中的名称以便允许使用重音符号(é、á、...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6999772/

相关文章:

jquery - 如何禁用单个 View 的客户端验证?

c# - WPF中ViewModels之间如何通信以及如何控制Views生命周期

javascript - 从包含 javascript 中的 html <BR> 的字符串中解析地址

c# - 在初始页面加载时从服务器获取类对象并将其放入 js 对象的好方法是什么?

python - 在 Python 中是否有任何等同于 Perl 正则表达式的\K 反斜杠序列?

javascript - 正则表达式替换字符串中不是数字或句点的任何内容

asp.net-mvc - 使用 ServiceStack 服务与 ASP.NET MVC Controller 的优缺点?

c# - GridView 中的背景颜色

c# - wcf 返回 XmlDocument?

c# - 在 Linq to Sql 查询中获取默认值类型 (DateTime),结果为空