Check only rooms

In one of my rail models, I have this check :only_integer:

validates :number, presence: true, numericality: { only_integer: true }

This check also allows for inputs such as +82938434with +-signs.

Which check should be used only for input permissions without + -just numbers?

+4
source share
1 answer

The documentation for only_integermentions this regular expression:

/\A[+-]?\d+\z/

This means that you can simply use:

validates :number, format: { with: /\A\d+\z/, message: "Integer only. No sign allowed." }
+5
source

Source: https://habr.com/ru/post/1676611/


All Articles