1

My rails app have a field where users can enter their contact number and I'm using tel: like this to display:

<%= link_to sample.phone, "tel:#{sample.phone}" %>

So if the user entered a number in the phone field like this: 0176-40206387, the display will also be 0176-40206387

And if the user entered a number in the phone field like this: 017640206387, the display will also be 017640206387

As a newbie, I do not know which format a mobile phone can call, or will the phone automatically converts the number to a callable number? If not, what correct format is callable to tell the user how to enter their phone number?

  • 1
    This should help I guess http://stackoverflow.com/q/13662175/4587148 – Sajan Oct 20 '16 at 14:34
  • @sajan so my understanding now is that I should make a helper to convert the number inputted by the user to a callable number like the last 2 formats in the link you give? –  Oct 20 '16 at 14:54

2 Answers2

1

You should consider use number_to_phone

Also, you can check this link to help you how to make phone calls in ruby

  • So with these 2 solutions you gave, it doesn't matter anymore how the users entered their phone number in the field because these 2 solutions will automatically convert that number into a callable number? –  Oct 20 '16 at 14:43
-1

If you are going to be collecting phone numbers from users you need to make sure that you strip it of white space and prior to storage with something like this

sample.phone.gsub(/\D/, '')

That way you store the raw phone number.

Then in the view you can use the number_to_phone view helper like Lucas mentioned

number_to_phone( sample.phone, area_code: true)

If you want you could even just store the formatted phone number so you don't have to format it every time you want to view it.

C dot StrifeVII
  • 1,885
  • 1
  • 16
  • 21
  • should I make my user enter their phone number without any formatting i.e. spaces, dashes, +... then apply the number_to_phone solution or the other link that @Lucas Batista Gabriel mentioned ? –  Oct 20 '16 at 14:56
  • So you can try to enforce formatting upon input, but sanitizing user input to some sort of known state is usually the best options because you run into less unknowns and don’t have to trouble shoot down the line. You can also have a little more assurance that your data is uniform. – C dot StrifeVII Oct 20 '16 at 15:05