开发者

Locale sensitive character classes in regexp with validations?

开发者 https://www.devze.com 2023-01-19 04:31 出处:网络
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
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消