Anyone got a good validation for validating a phone number? I just want to make sure that whatever the user enters is somewhat sensical, and i''d like to be fairly liberal about what kind of a format it''s in. Joe
actually yes, check it out: validates_format_of :billing_phone, :billing_fax, :with => /[0-9]{3}-[0-9]{3}-[0-9]{4}/, :message => "- Phone numbers must be in xxx-xxx-xxxx format." and heres one which lets the above work, but also lets parentheses be optional, ie (123)-123-1234 would work: validates_format_of :billing_phone, :billing_fax, :with => /\(?[0-9]{3}\)?-[0-9]{3}-[0-9]{4}/, :message => "- Phone numbers must be in xxx-xxx-xxxx format." -- Scott Becker Web Developer Electro Interactive www.ElectroInteractive.com On 2/2/06, Joe Van Dyk <joevandyk@gmail.com> wrote:> Anyone got a good validation for validating a phone number? I just > want to make sure that whatever the user enters is somewhat sensical, > and i''d like to be fairly liberal about what kind of a format it''s in. > > Joe > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hm, neat. Thanks. That does force the user into putting their phone number into a specific format, which is generally a usability no-no, right? Here''s what I came up with: def validate validate_phone_number :home_phone validate_phone_number :cell_phone end private def validate_phone_number type_of_number number = self.send type_of_number return if number.blank? errors.add(type_of_number, "contains a letter") if number.match(/[a-zA-Z]/) digits_only = number.gsub(/[^\d]/, '''') errors.add(type_of_number, "not long enough") if digits_only.size < 10 errors.add(type_of_number, "too long") if digits_only.size > 11 end In my case, the user doesn''t have to submit a phone number, so I quit if the phone number is blank. Otherwise, I check to see if there''s a letter in the number and report an error if it does. I then strip all non-digits from the phone number. I then check to see if the size of the digits in the number is either 10 or 11. Does that look ok? I think I can take in phone numbers in pretty much any format the user cares to enter it in by. On 2/2/06, Scott Becker <becker.scott@gmail.com> wrote:> actually yes, check it out: > > validates_format_of :billing_phone, :billing_fax, > :with => /[0-9]{3}-[0-9]{3}-[0-9]{4}/, > :message => "- Phone numbers must be in xxx-xxx-xxxx format." > > and heres one which lets the above work, but also lets parentheses be > optional, ie (123)-123-1234 would work: > > validates_format_of :billing_phone, :billing_fax, > :with => /\(?[0-9]{3}\)?-[0-9]{3}-[0-9]{4}/, > :message => "- Phone numbers must be in xxx-xxx-xxxx format." > > -- > Scott Becker > Web Developer > > Electro Interactive > www.ElectroInteractive.com > > > On 2/2/06, Joe Van Dyk <joevandyk@gmail.com> wrote: > > Anyone got a good validation for validating a phone number? I just > > want to make sure that whatever the user enters is somewhat sensical, > > and i''d like to be fairly liberal about what kind of a format it''s in. > > > > Joe > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
You can also restrict it at the client side so that only a phone number format can be entered instead of making the round trip to discover they messed up. Nothing "bad" about input masks unless you need more than US phone numbers. http://javascript.internet.com/forms/dfilter.html Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Joe Van Dyk > Sent: Thursday, February 02, 2006 9:18 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] validating phone numbers > > Hm, neat. Thanks. That does force the user into putting their phone > number into a specific format, which is generally a usability no-no, > right? > > Here''s what I came up with: > > def validate > validate_phone_number :home_phone > validate_phone_number :cell_phone > end > > private > > def validate_phone_number type_of_number > number = self.send type_of_number > return if number.blank? > errors.add(type_of_number, "contains a letter") if number.match(/[a- > zA-Z]/) > digits_only = number.gsub(/[^\d]/, '''') > errors.add(type_of_number, "not long enough") if digits_only.size < 10 > errors.add(type_of_number, "too long") if digits_only.size > 11 > end > > In my case, the user doesn''t have to submit a phone number, so I quit > if the phone number is blank. > > Otherwise, I check to see if there''s a letter in the number and report > an error if it does. I then strip all non-digits from the phone > number. I then check to see if the size of the digits in the number > is either 10 or 11. > > Does that look ok? I think I can take in phone numbers in pretty much > any format the user cares to enter it in by. > > On 2/2/06, Scott Becker <becker.scott@gmail.com> wrote: > > actually yes, check it out: > > > > validates_format_of :billing_phone, :billing_fax, > > :with => /[0-9]{3}-[0-9]{3}-[0-9]{4}/, > > :message => "- Phone numbers must be in xxx-xxx-xxxx format." > > > > and heres one which lets the above work, but also lets parentheses be > > optional, ie (123)-123-1234 would work: > > > > validates_format_of :billing_phone, :billing_fax, > > :with => /\(?[0-9]{3}\)?-[0-9]{3}-[0-9]{4}/, > > :message => "- Phone numbers must be in xxx-xxx-xxxx format." > > > > -- > > Scott Becker > > Web Developer > > > > Electro Interactive > > www.ElectroInteractive.com > > > > > > On 2/2/06, Joe Van Dyk <joevandyk@gmail.com> wrote: > > > Anyone got a good validation for validating a phone number? I just > > > want to make sure that whatever the user enters is somewhat sensical, > > > and i''d like to be fairly liberal about what kind of a format it''s in. > > > > > > Joe > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 3/02/2006, at 6:22 PM, Bob Silva wrote:> Nothing "bad" about input masks unless you need more than US > phone numbers.There''s everything bad about input masks - it forces the user to input your way, not how they''re used to. Imagine if every form came with a handwriting mask so you wrote their way... Take these for example: 555-555-1234 (555) 555 1234 555.555.1234 They all contain the same information, and each person writes it their own way. All you need is 5555551234, so let the person input what they want and check that. I work in a company that deals with things like this all the time, we just let people enter whatever they like - after all, the numbers will be read by another human, not a machine. -- Phillip Hutchings phillip.hutchings@sitharus.com http://www.sitharus.com/
On Thursday, February 02, 2006, at 9:18 PM, Joe Van Dyk wrote:>Otherwise, I check to see if there''s a letter in the number and report >an error if it does. I then strip all non-digits from the phone >number. I then check to see if the size of the digits in the number >is either 10 or 11.One thing to consider, though, is the inclusion of an extension in the phone number. eg/ 555-555-5555 x555 In this scenario, the "x" breaks the letter check, and the extra 3 digits break the 10 or 11 digit check. I suppose you could just have them enter an extension into another box, if needed. That simplifies things. Then, of course, there are international numbers... clay. -- Posted with http://DevLists.com. Sign up and save your time!
On 3 Feb 2006 14:58:43 -0000, Clay Harmony <bounce@devlists.com> wrote:> > On Thursday, February 02, 2006, at 9:18 PM, Joe Van Dyk wrote: > >Otherwise, I check to see if there''s a letter in the number and report > >an error if it does. I then strip all non-digits from the phone > >number. I then check to see if the size of the digits in the number > >is either 10 or 11. > > One thing to consider, though, is the inclusion of an extension in the > phone number. > eg/ 555-555-5555 x555Hm... good point. I forgot about that.> In this scenario, the "x" breaks the letter check, and the extra 3 > digits break the 10 or 11 digit check. I suppose you could just have > them enter an extension into another box, if needed. That simplifies things. > > Then, of course, there are international numbers...What do those generally look like?
On Friday, February 03, 2006, at 9:56 AM, Joe Van Dyk wrote:>> Then, of course, there are international numbers... > >What do those generally look like?That''s the tough part. The general format is: (country code) (city/area code) (phone number) The country code for the US is 1. (Canada is 1 also... somehow we got lumped together.) Country codes can be anywhere from 1 to 3 (4?) digits. City/area codes... I have no idea. The final phone number... also no idea. Each country is a little different. I used to live in Jakarta, Indonesia... The country code for Indonesia is (62). City code is (21). Phone numbers could be 6-8 digits. (Jakarta is a huge city... originally all numbers were 6 digits, and then they started handing out 7 digit numbers. They quickly ran out of those and started using 8 digit numbers. Clearly, they need to start planning ahead.) So, my old phone number was: +62-21-781-5555 As with phone extensions, it''s probably best to have the country and city/area codes entered in a separate box. To add a little more confusion to the mix, though... people don''t always fully understand how to translate their local number into an international number. Taking the Indonesia example again... dialing within Indonesia, you didn''t need to use the country code. But, for some reason, you had to prepend a ''0'' onto your city code. So, people would write their phone number like this: 021-781-5555 So, you''d have to know to drop that ''0'' when writing your phone number for international consumption. I''m fairly certain this method is employed in other countries, too. Point of all this is... international numbers are tricky, and it would be a non-trivial excercise writing a filter intelligent enough to correctly identify an "incorrect" number. clay. -- Posted with http://DevLists.com. Sign up and save your time!
On 4/02/2006, at 6:56 AM, Joe Van Dyk wrote:> What do those generally look like?A string of numbers starting with a +. It''s generally like this: +[0-9]{1,4} [0-9]{2,5} [0-9]{4-9} (x[0-9]{1-5})?, but it''s just easier to allow free text, people won''t try anything unless they have a reason to, and in that case they''d generally lie anyway. This is one case where user education is often needed. When you convert your number to an international form you drop the dialling prefix (normally 0 or 1, most countries use 0) and add a + and your country code. + being replaced by the international dialling prefix, normally 00 or 11. You''d be surprised how many people don''t know the format or their country code. -- Phillip Hutchings phillip.hutchings@sitharus.com http://www.sitharus.com/
On 3 Feb 2006, at 19:44, Clay Harmony wrote:> > On Friday, February 03, 2006, at 9:56 AM, Joe Van Dyk wrote: >>> Then, of course, there are international numbers... >> >> What do those generally look like? > > That''s the tough part.Nah, you''ve over-complicated it, I think. There is an international standard for this. Here''s the industry standard for writing phone numbers: +XXYYYYPPPPPPP + means "dial the number for an international number, e.g. ''0'' if the country code doesn''t match the country you''re dialling from" XX is the country code YYYY is the area code and can be 2, 3, 4, 5... digits long, depending on locale PPPPPPP is the number within the area, and again can be any length. Generally, not including the country code it is rare to see a number longer than 11 digits. If it is 11 digits, chances are 4 of those are area code. If it''s 10, 3 of them are likely to be area code. This is because local phone numbers are rarely more than 7 digits because Bell realised that humans are no good at remembering patterns longer than 7 digits (evolutionary psychology has shown this is related to the ratio of a part of the neural cortex to another part of the brain, IIRC, but I digress). As a result, everybody else in the World followed this format (for much the same reason why nearly all railways in the World are the width of an 1850''s Newcastle coal cart...) For example, I will reject a shopping cart if it doesn''t let me enter my mobile as beginning +447740... All my friends numbers in my mobile are stored in this format, and yes mobile phones are smart enough to work it all out, so if you''re in the US you can store your numbers as +1... The advantage? When I''m in the UK all my numbers in +44 format still just work. When I travel abroad, the phone and network have a little cuddle, work it out, and I don''t need to work out what''s going on - they all still "just work". That format should be the format all numbers are stored in if you ask me. If somebody enters a number (07740) 23... in my site, I automatically convert it to +44774023... I would advise others do the same unless you''re dealing with people who need to see it in a "domestic" format. -- Paul Robinson