0

I'm trying to add validation to the mobile number field. I don't want the user to input anything other than integers in this field. So, I used the following:

validates :mobile, numericality: { only_integer: true }

This works when I enter just the plus sign in the field and an error is promptly displayed. However, if you enter +123 or any other number immediately following the plus sign, the entry is saved without throwing an error. How do I ensure that only whole numbers are accepted?

jasonng
  • 107
  • 1
  • 7

1 Answers1

1

You validate the field, according the Rails documentation as follows:

validates :mobile, format: { with: /\A\d+\z/ }

However: why don't you allow usage +, -, (, ), and spaces in a mobile number? If you allow them to match, please refer to the topics: 1, and 2.

Community
  • 1
  • 1
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69