validates_format_of :first_name, :with => /\A\w+\Z/
The validation doesn't pass if there are non-english character in first name, like Mölläinen
. Changing Rails locale doesn't help. So how do you do culture/locale sensitive validation then?
If the locale is EN
, then \w
sho开发者_开发知识库uld map to [a-zA-Z0-9_]
, but if it's FI
, then it should map to [a-zA-Z0-9_äöåÄÖÅ]
and so on.
Try /\A[[:alpha:]]+\Z/
. This should be locale aware, at least in Ruby 1.9.
But you might want to allow other characters too. Anna-Lena
is a common name in Germany, for example.
精彩评论