Does anyone have any good 'phone' validation routines?

I'm on the final stages of a large project and am now going through all the model validations. I've got many of them and am happy to share if anyone's in need. I'd like to find a good, practical PHONE validation if anyone has one and is willing to share. I've done an exhaustive Google search and surprisingly, they elude me??? Thank you, Kathy

I'm on the final stages of a large project and am now going through all the model validations. I've got many of them and am happy to share if anyone's in need. I'd like to find a good, practical PHONE validation if anyone has one and is willing to share. I've done an exhaustive Google search and surprisingly, they elude me??? Thank you, Kathy

I usually just remove all the punctuation, then make sure I have the proper number of digits left.

phone = '(615) 867-5309'

=> "(615) 867-5309"

phone.gsub( /[^\d]/, '' ).size == 10

=> true

That's what I do too, and it works in my case, but it can be ambiguous. For instance, using the + convention for international codes,

+506 123-4567 From a US point of view that's a phone number in Costa Rica, while

(506) 123-4567 is a phone number in Canada.

It's a stretch, admittedly, but something to be aware of.

I wrote this yesterday. Not so much a validation, but rather a PhoneNumber class. Strips all non-numerics upon instantiation. Intended to store the number as a packed (unformatted) string of digits and then to be formatted upon display (so your app has a uniform display of humbers from all input sources). Has a few different formatting options. Optimized for US style numbers at this point.

If you're just interested in the part that strips the non-numerics, see the method near the top.

-- gw (www.railsdev.ws)

class PhoneNumber

Hi --

I'm on the final stages of a large project and am now going through all the model validations. I've got many of them and am happy to share if anyone's in need. I'd like to find a good, practical PHONE validation if anyone has one and is willing to share. I've done an exhaustive Google search and surprisingly, they elude me???

I wrote this yesterday. Not so much a validation, but rather a PhoneNumber class. Strips all non-numerics upon instantiation. Intended to store the number as a packed (unformatted) string of digits and then to be formatted upon display (so your app has a uniform display of humbers from all input sources). Has a few different formatting options. Optimized for US style numbers at this point.

If you're just interested in the part that strips the non-numerics, see the method near the top.

-- gw (www.railsdev.ws)

class PhoneNumber

#------------------------------------------------------------ def initialize(chars='')   self. chars end

def set(chars)   self. chars end

def (chars)   charSet = chars.dup.split("")   charSet.delete_if do |char|     (char =~ /\d/) == nil   end   @phoneNumber = charSet.join end

You're doing way too much work :slight_smile: This should give you the same thing:

   def (chars)      @phone_number = chars.gsub(/\D/, "")    end

(And cAmElCaSe removal thrown in free :slight_smile:

The use of to set the phone number seems a little odd to me. Anyway....

#------------------------------------------------------------ def formattedWith(format='-')

# formats any number with at least 7 digits and a maximum of 16

  formattedNumber = String.new   numberLength = @phoneNumber.size

  if (numberLength == 7)     @phoneNumber = '000' + @phoneNumber   elsif (numberLength < 7) || (numberLength == 8) || (numberLength == 9) || (numberLength > 16)     return @phoneNumber   end

  if format == '('

    if @phoneNumber[0,3] != '000'       formattedNumber += '(' + @phoneNumber[0,3] + ') '     end     formattedNumber += @phoneNumber[3,3]     formattedNumber += '-'     formattedNumber += @phoneNumber[6,4]

  elsif format == '/'

    if @phoneNumber[0,3] != '000'       formattedNumber += @phoneNumber[0,3] + '/'     end     formattedNumber += @phoneNumber[3,3]     formattedNumber += '-'     formattedNumber += @phoneNumber[6,4]

  else

    if @phoneNumber[0,3] != '000'       formattedNumber += @phoneNumber[0,3] + format     end     formattedNumber += @phoneNumber[3,3]     formattedNumber += format     formattedNumber += @phoneNumber[6,4]

  end

Here you're working too hard, too, and repeating too much code. I understand that you want to keep the different cases separate, so that it's easier to add new cases (rather than come up with some very intricate way to construct the string that would be too easy to break). Even with that in mind, you could get this much more consolidated and unrepetitive:

   if format == '('       ac = '(' + areacode + ') '       sep = '-'

    elsif format == '/'       ac = areacode + '/'       sep = '-'

    else       ac = areacode + format       sep = format

    end

    formatted_number << ac if has_areacode?     formatted_number << join_exchange_and_number(sep)

Or if you wanted to you could use a case statement, and/or do the assignments directly from the case or conditional:

   ac, sep = case format              when '(' then ['('+areacode + ') ', '-' ]              when '/' then [areacode + '/', '-' ]              else [areacode + format, format ]    end

   formatted_number << ac if has_areacode?    formatted_number << join_exchange_and_number(sep)

That would be easy to add cases to. And of course you would need:

   def join_exchange_and_number(sep='-')      @phone_number[3,3] << sep << @phone_number[6,4]    end

   def has_area_code?      areacode != '000'    end

Note: I've only tested these code snippets to the extent of looking quickly at screen output of your examples. Hopefully they'll give you some useful ideas.

David

def (chars)   charSet = chars.dup.split("")   charSet.delete_if do |char|     (char =~ /\d/) == nil   end   @phoneNumber = charSet.join end

You're doing way too much work :slight_smile: This should give you the same thing:

   def (chars)      @phone_number = chars.gsub(/\D/, "")    end

(And cAmElCaSe removal thrown in free :slight_smile:

Yeah, Ruby does regex stuff like this different than my prev language, so I still have some cause & effect experimenting to do. Plus, I have blocks on the brain of course. Thanks for this one. Cleaner for sure :slight_smile:

I absolutely despise underscores between words in variables -- that one is going to be a very hard one for me, and for the moment I am ignoring it. I'm sure after I've spent more time looking at Ruby code, I'll get more accustomed to it. At the moment, it's slows me down too much to be constantly fixing typos because I instinctually camel case. I'll get over myself eventually.

The use of to set the phone number seems a little odd to me.

As the entirely of the object data is just a simple string, I was trying to find a way to not have to use an attribute during assignment so it felt like the assignent was to the object as a whole just like a string would be.

I'm still muddling my way through what Ruby can do (and of course what should be done regardless of the "can"), and experimenting with how to make things look & feel a certain way in Ruby.

Anyway....

  if format == '('

    if @phoneNumber[0,3] != '000'       formattedNumber += '(' + @phoneNumber[0,3] + ') '     end     formattedNumber += @phoneNumber[3,3]     formattedNumber += '-'     formattedNumber += @phoneNumber[6,4]

Here you're working too hard, too, and repeating too much code.

Yeah, I know. I started making cases, and hadn't gotten around to reducing them. Brand new to Ruby, so not yet equipped to solve every instance of "hard work" all at the same time. I'll focus on objective X even if that leaves objective Y hacked up for the time being. I actually should have known better than to leave in that particular dose of obvious repetitive coding, but I just wasn't focused on that part of it.

Note: I've only tested these code snippets to the extent of looking quickly at screen output of your examples. Hopefully they'll give you some useful ideas.

Yes, I appreciate the critique -- thanks.

-- gw (www.railsdev.ws)