代码之家  ›  专栏  ›  技术社区  ›  timpone

在Rails中禁用单个字符(<)的验证

  •  0
  • timpone  · 技术社区  · 7 年前

    我不允许在我们的模型中使用名字中的<字符。雷杰克斯会做什么?

    我试过

      validates :first_name, format: { with: /[^<]/ }
    

    但似乎不起作用。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Matt    7 年前

    你可以使用 without 而不是 with 在验证中
    你可能想逃出 zishe suggests 但我不认为这是必要的。

    validates :first_name, format: { without: /</ }
    

    https://guides.rubyonrails.org/active_record_validations.html#format

    edit:incorporated@engineersmnky points,简单得多。

        2
  •  0
  •   inyono    7 年前

    您当前只捕获第一个字符。所以你可能想做点什么 [^<].* (一个以除 < 后面是任意字符)