Guys, I have a text field and the name is phone I used to enter contact number. In the new form when i enter 10 digit number like "1234567890" it is displaying 1234567890 I want to display like in US format automatically like "(123) 456-7890". I need to provide validation for that field also. Can u help me how can i do that. Very badly i need a help. i am struck with this issue...... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
cool wrote:> Guys, > > I have a text field and the name is phone > I used to enter contact number. > In the new form when i enter 10 digit number like "1234567890" it > is displaying 1234567890 > I want to display like in US format automatically like "(123) > 456-7890". > I need to provide validation for that field also. > Can u help me how can i do that. > Very badly i need a help. i am struck with this issue......validates_format_of :phone, :with => ((\(\d{3}\) ?)|(\d{3}[- \.]))?\d{3}[- \.]\d{4}(\s(x\d+)?){0,1}$ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you could simply write your own helper like that: def format_phone_number(number) return "(#{number[0..2]})#{number[3..5]}-{number[6..-1]}" end add it to your view_helper and call it whenever you like. validations are similar. just add your own validation. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m not sure the type of validation you need on the field. This is something simple I did for an application I wrote: phone =~ /^\d{10}$/ && phone.to_i >= 999999999 This will make sure that the string contains only 10 digits (/^\d {10}$/) and that the 10 digits are miningful or the number doesn''t start with a 0 (>= 999999999). About the formatting of the string, rails has tons of helpers, and the one you''re looking for is number_to_phone: <%= number_to_phone(1234567890, :aea_code => true, :delimiter => '' '') %> # (123) 456-789 On Mar 2, 2:52 am, cool <eshward...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Guys, > > I have a text field and the name is phone > I used to enter contact number. > In the new form when i enter 10 digit number like "1234567890" it > is displaying 1234567890 > I want to display like in US format automatically like "(123) > 456-7890". > I need to provide validation for that field also. > Can u help me how can i do that. > Very badly i need a help. i am struck with this issue......--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, made a mistake, the result of number_to_phone would be (123) 456-7890 (I missed the 0). And about the number of digits, I said \d{10} makes sure the string contains only 10 digits. What I meant is that the string will have to contain a total of 10 digits, no more, no less. Pepe On Mar 2, 10:53 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> I''m not sure the type of validation you need on the field. This is > something simple I did for an application I wrote: > > phone =~ /^\d{10}$/ && phone.to_i >= 999999999 > > This will make sure that the string contains only 10 digits (/^\d > {10}$/) and that the 10 digits are miningful or the number doesn''t > start with a 0 (>= 999999999). > > About the formatting of the string, rails has tons of helpers, and the > one you''re looking for is number_to_phone: > > <%= number_to_phone(1234567890, :aea_code => true, :delimiter => '' '') > %> # (123) 456-789 > > On Mar 2, 2:52 am, cool <eshward...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Guys, > > > I have a text field and the name is phone > > I used to enter contact number. > > In the new form when i enter 10 digit number like "1234567890" it > > is displaying 1234567890 > > I want to display like in US format automatically like "(123) > > 456-7890". > > I need to provide validation for that field also. > > Can u help me how can i do that. > > Very badly i need a help. i am struck with this issue......--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---