2009-07-29 44 views

回答

3

更多railsy方式是在模型中自動驗證

validate :yourvalue_must_contain_at_least_3_alphabetic_characters 

protected 

def yourvalue_must_contain_at_least_3_alphabetic_characters 
errors.add(:yourvalue, 'should have at least 3 alphabetic characters') if yourvalue.gsub(/[^A-Z]/i,"").size > 2 
end 
7

雖然我更喜歡DanSingerman的解決方案,你也可以去純粹的正則表達式基礎:

validates_format_of :password, :with => /([^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*){3,}/ 
相關問題