Advice on how to validate names and surnames using regex
I want to validate name and surname for my Ruby on Rails 3 application and so I have posted this question. Someone advi开发者_运维问答ced me to read the Falsehoods Programmers Believe About Names article and now I am in trouble.
What is the right way to validate names and surname?
Regex is a way, but what should I be careful? Can you give me an overview?
The correct way to validate names is:
- Accept any and all possible Unicode characters.
- Reject inputs of length 0.
- 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.
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?
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.
精彩评论