Topic: Phone number regex [SOLVED]

i am trying to write a regex that would match true if it found any of this types of this phone numbers

  https://github.com/btelles/faker/blob/m … _number.rb

   '###-###-####',
   '(###)###-####',
   '1-###-###-####',
   '###.###.####',
   '###-###-####',
   '(###)###-####',
   '1-###-###-####',
   '###.###.####',
   '###-###-#### x###',
   '(###)###-#### x###',
   '1-###-###-#### x###',
   '###.###.#### x###',
   '###-###-#### x####',
   '(###)###-#### x####',
   '1-###-###-#### x####',
   '###.###.#### x####',
   '###-###-#### x#####',
   '(###)###-#### x#####',
   '1-###-###-#### x#####',
   '###.###.#### x#####'

Last edited by moiseszaragoza (2012-07-13 14:49:12)

Re: Phone number regex [SOLVED]

Does this work? Just wrote it, didn't test:

(1)?(?:-)?(?:\(|-)?([\d]{3})(?:\.|\-|\))([\d]{3})(?:\.|\-)([\d]{4})(?: ?x([\d]{3,5}))?
Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Phone number regex [SOLVED]

Thanks

 validates_format_of :office,                                                                                                                                                                                    
      :with => %r{(1)?(?:-)?(?:\(|-)?([\d]{3})(?:\.|\-|\))([\d]{3})(?:\.|\-)([\d]{4})(?: ?x([\d]{3,5}))?},
       :message => "should be a phone number"

Last edited by moiseszaragoza (2012-07-13 15:11:36)