This article is based on the Rails documentation. https://railsdoc.com/validation#validates_format_of
with_options By writing with_options 〇〇 do ~ end, it is possible to add the same options to multiple pieces of information at once.
This option is available when using the validates method in a model class. ** format: {with: regular expression, message: error message when the regular expression is not matched} ** By doing so, you can limit a specific column using a regular expression.
As for regular expressions, the basic thing is, "If you have this, you'll be fine for the time being." Please refer to it if you like! https://qiita.com/teipoi/items/8a28a56bc584780b8d8a
In the following, three validations can be restricted to "presence". In addition, the format option is used to set validation with regular expressions for names and reading kana.
apps/model/user.rb
with_options presence: true do
validates :name, format: { with: /\A[Ah-Hmm-One-龥]/, message: "is invalid.
Input full-width characters." }
validates :age, format: { with: /\A[0-9]+\z/, message: "is invalid.
Input half-numbers." }
validates :nickname, format: { with: /\A[a-z0-9]+\z/i, message: "is invalid.
Input half-width characters." }
end