3

I am new to Ruby on Rails, looking for any suggestions to validate a phone number to be xxx-xxx-xxxx. Appreciate any suggestions. My phone_number column type is a string. Thanks!

Deepak
  • 39
  • 1
  • 1
  • 2

1 Answers1

6

If you need it to validate phone field in model, you can use this code:

validates :phone_number, format: { with: /\d{3}-\d{3}-\d{4}/, message: "bad format" }
ExWei
  • 350
  • 3
  • 8