Adding a zero to the front of a telephone number

  • Filter
  • Time
  • Show
Clear All
new posts

    Adding a zero to the front of a telephone number


    I wonder if anyone can advise a Rails novice on how to achieve the following.

    I am creating a contacts database and telephone numbers are part of it, the majority of which will be UK telephone numbers along with a small percentage of Irish telephone numbers.

    I have defined the telephone number in the schema as an integer which means that when i go to display the telephone number it displays without a zero at the beginning and no other formatting.

    I have discovered the number_to_phone() helper which is currently formatting a number entered into the database as 08453226838 as 845-322-6838 which is great but it drops the leading zero!!

    Does anyone know how i can add the leading zero?

    On a separate note, Irish numbers would be typed into the system starting with 00353 so i need help in understanding how to say If '353' add '00' in place of the above leading zero.

    Hope that makes sense, any help is much appreciated.

    It would be better if you defined the telephone number as a text field. Then it can be displayed as text so you don't have to worry about leading zeros.

    The point about numbers is that you can do mathematical calculations - ie add, substract, find averages etc.

    As a rule you do not need to add telephone numbers togethers, or find the average phone number of all your customers etc. Therefore you do not need to define these as numbers. Text is fine.

    Also, if you are storing in a database you simply defined the field as say varchar(15) and don't have to worry about long ints or medium ints, signed or unsigned etc.


      Hi Paulmann,

      Thank you for taking the time to advise me, much appreciated.

      I can see the advantages and logic behind using text and will probably end up doing it this way. My concerns were based around controlling the data going into the database. I didn't want the people using it being able to type in anything other than numbers and the number_to_phone() helper, along with validates_numericality_of seemed a good and sensible starting place for controlling the format.

      I will take your advice and look into ways of controlling what goes into the database and how it is then displayed.

      Thank you


        Don't make it too difficult for yourself. People type phone numbers in many different ways (01234)567890, (01234) 567890, 01234-567890, 01234-567-890, or with commas or periods or any number of other characters. If they do it in the way they think is correct and then get an error message they will probably not bother and go somewhere else.

        You've also got to think about overseas customers. You might not have any now but maybe you will later. They might put in an international dialling code like +39 1234567890 or USA 555 2345 or any number of combinations.

        I don't know much about ruby so can't say what the number to phone helper does but I think it only deals with US numbers anyway.


          Many thanks for your time and help on this issue Paulmann.