嘿...你如何验证完整姓名字段(名字和姓氏)。
你可能只想确保某些字符不存在,而不是验证那里有哪些字符。
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
validates_format_of :name, :with => /^[^0-9`!@#\$%\^&*+_=]+$/, :multiline => true
- barek2k2\A
和\z
。感谢您的评论。 - maček------
。 - ExcellentAverage在这里进行的任何验证,除非极为普遍,否则很可能会崩溃。例如,强制最小长度为3可能是您可以做到而不涉及输入内容的特定性的最合理选择。
当您有像带撇号的“O'Malley”,带破折号的“Smith-Johnson”,带重音字符的“Andrés”或几乎没有字符的“Vo Ly”等极短名称时,如何进行验证而不排除合法情况?这并不容易。
至少一个空格和至少4个字符(包括空格)
\A(?=.* )[^0-9`!@#\\\$%\^&*\;+_=]{4,}\z
#@%...
,数字,并且至少有一个空格。 - Draco Ater