0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

正規表現を使うバリデーション

Last updated at Posted at 2023-01-30

2.6 format

このヘルパーは、withオプションで与えられた正規表現と属性の値がマッチするかどうかを検証します。

class Product < ApplicationRecord
  validates :legacy_code, format: { with: /\A[a-zA-Z]+\z/,
    message: "英文字のみが使えます" }
end

:withoutオプションを使うと、指定の属性にマッチしない正規表現を指定することもできます。

デフォルトのエラーメッセージは「is invalid」です。

出典

感想

正規表現が大変だった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?