验证特殊字符

时间:2010-05-29 08:15:43

标签: ruby-on-rails

我想将ruby中的验证放在rails上,我的用户名不应包含特殊字符!@#$%^ *()_- + = /<>?:'“;。 请告诉我如何在我的代码中实现它。

2 个答案:

答案 0 :(得分:3)

按照@Voyta

的建议使用您的模型validates_format_of

我在我的例子中添加了一个正则表达式:

validates_format_of :username, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/

答案 1 :(得分:1)

使用validates_format_of(请参阅有关正则表达式示例的注释):

validates_format_of :username, :with => /\Ayour_regex_here\Z/i