【问题标题】:How do i forbid a character in rails我如何禁止rails中的字符
【发布时间】:2012-08-01 09:07:13
【问题描述】:

我想禁止字段中出现“/”。

我在文档中读到它应该是这样的:

valid_name_regex = /
验证:名称,存在:真, 唯一性:{ case_sensitive: true },格式:{ with: valid_name_regex }

但我想要相反的。

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby regex validation


    【解决方案1】:

    试试类似的东西

    validates :name, presence: true, uniqueness: { case_sensitive: true }, format: { with: /^[^\/]+$/ }
    

    【讨论】:

    • Regexp 将在空字符串上失败,并将错误添加到模型的错误中。
    • 我会使用%r{^[^/]*$} 作为正则表达式。备用文字语法使转义斜线不必要,* 而不是+ 使其适用于空字符串。
    • 这个正则表达式模式/^[^\/'"]+$/怎么样?这将不允许反斜杠和空字符串
    • 谢谢大家,我用的是Gaurish的sn-p
    猜你喜欢
    • 1970-01-01
    • 2015-01-18
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多