Advice on how to validate names and surnames using regex

前端 未结 3 1392
我在风中等你
我在风中等你 2021-01-07 05:22

I want to validate name and surname for my Ruby on Rails 3 application and so I have posted this question. Someone adviced me to read the Falsehoods Progra

相关标签:
3条回答
  • 2021-01-07 05:44

    The simple answer is, you cannot validate names and surnames other than to verify they are not null*. By filling in a form that asks for a name, somebody is declaring "this is my name", and if someone says "this is my name", you should believe them because they are the ultimate authority of what is their name.

    * even then, it's perfectly valid for people to have a single name rather than two or three, and there has been one instance of an individual who didn't actually have a name. He gave himself a symbol instead.

    0 讨论(0)
  • 2021-01-07 05:54

    If you have read that article, you have seen that it is not possible to validate a name, with or without regular expressions.

    So don't even go there. If people want to call themselves Tarquin Fin-tim-lin-bin-whin-bim-lim-bus-stop-F'tang-F'tang-Olé-Biscuitbarrel (it has happened! see the section "Cultural references"), who are you to deny it to them?

    0 讨论(0)
  • 2021-01-07 05:55

    The correct way to validate names is:

    1. Accept any and all possible Unicode characters.
    2. Reject inputs of length 0.
    3. Make sure the result has at least one character that is neither of type \pM, \pZ, nor \pC; i.e., is neither one of the combining marks, separators, nor other (usually control) characters. A character class like [^\pM\pC\pZ] should suffice.

    There: easy as can be. You will probably need Ruby 1.9 to do this properly, though.

    0 讨论(0)
提交回复
热议问题