用于外国名称的 Javascript 正则表达式

标签 javascript regex naming

我做了一个简单的正则表达式来验证名字,我没想到的是一些用户有两个(或更多不常见的名字)并且他们有像 áéióú 这样的重音符号。

我也在考虑其他字符,例如西类牙语 ñ 或 ç。

到目前为止,这是我的验证码:

function validateForm(element) {
    var regex =  /^[a-zA-Z\-]+$/;
         var ctrl =  document.getElementById(element).value;

        if(ctrl == null || ctrl == '')
            return;

         if (!regex.test(ctrl)) {

         alert(element + ' not valid');
         document.getElementById(element).focus();
}

我不知道该怎么做,有什么提示吗?

最佳答案

我不会引用全部 Falsehoods Programmers Believe About Names ,但足以说明一个简单的正则表达式无法准确捕捉人名的复杂性。

对于您提出的每条规则,保证有数百个异常(exception)。它的“名字不能有空格”除了“van Buren”,名字只有字母除了O'Reily,名字只有一个大写字母除了“McDonnell” 等等,甚至一个人会有名字和姓氏除了“Cher”、“Prince”、“Bono”等...

如第一条评论所述,名称唯一可能的正则表达式是:

/^.+$/

还有麻烦的事件,因为它意味着一个名字甚至有一个书面形式开头。


为了后代,我将在此处列出这篇文章中的谎言:

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.

关于用于外国名称的 Javascript 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33948270/

相关文章:

excel - 当工作表名称为数字时,索引号/工作表名称与变量混淆

javascript - 使用 .call(this) 继承原型(prototype)

javascript - 如何在减少宽度和高度时停止文本在 div 内更改(灵活填充)

javascript - 有没有办法阻止隐藏文本在加载时出现然后消失?

regex - 是否可以排除 apache 访问日志中指定的 GET 参数?

javascript - 计算文本区域中的特定字符

安卓工作室 : Regex not working

java - 点击 map 获取多个坐标

haskell - "map head . group"有名字吗?

python - 做Python喜欢 Camel