レールの妥当性を検証full_name
-
26-09-2019 - |
質問
こんにちは...ょうかを検証するfull_name分野(姓名).
解決
考えてい名前:
- さんJan Levinson-グールド
- 博士Martin Luther King Jr.
- Brett d'Arras-d'Haudracey
- Brüno
の代わりに検証されている文字がするという確保の一部の文字まで ない ます。
例えば:
class User < ActiveRecord::Base
validates_format_of :full_name, :with => /\A[^0-9`!@#\$%\^&*+_=]+\z/
# add any other characters you'd like to disallow inside the [ brackets ]
# metacharacters [, \, ^, $, ., |, ?, *, +, (, and ) need to be escaped with a \
end
試験
Ms. Jan Levinson-Gould # pass
Dr. Martin Luther King, Jr. # pass
Brett d'Arras-d'Haudracey # pass
Brüno # pass
John Doe # pass
Mary-Jo Jane Sally Smith # pass
Fatty Mc.Error$ # fail
FA!L # fail
#arold Newm@n # fail
N4m3 w1th Numb3r5 # fail
正規表現の説明
NODE EXPLANATION
--------------------------------------------------------------------------------
\A the beginning of the string
--------------------------------------------------------------------------------
[^`!@#\$%\^&*+_=\d]+ any character except: '`', '!', '@', '#',
'\$', '%', '\^', '&', '*', '+', '_', '=',
digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\z the end of the string
他のヒント
(スペースを含む)少なくとも一つの空間と少なくとも4チャー
\A(?=.* )[^0-9`!@#\\\$%\^&*\;+_=]{4,}\z
あなたはここで行う任意の検証は、それは非常に一般的でない限り、ブレークダウンする可能性があります。例えば、3の最小長を強制することは、あなたが入力しているものの具体的に得ることなく得ることができるよう、おそらく程度として合理的である。
あなたは、アクセント付き文字、あるいはまったくほとんどない文字を「VoのLyの」、どのように非常に短い名前のダッシュ、「アンドレス」とアポストロフィ、「スミス・ジョンソン」で「オマリー」のような名前を持っている場合あなたが合法的な例を排除することなく検証するのですか?それは簡単ではありません。
所属していません StackOverflow