By Nathan L. Walls

  • .
  • Encased/Raleigh
  • .
  • Completed Snow Angel/Raleigh

Articles tagged “testing”

Ruby on Rails phone number validation

I recently launched a Rails application that has a phone number field available to it. In the process of writing my spec tests, I allowed the phone field to be a certain number of characters and allowed it to be empty. The formatting test, however, I left pending. I searched for, but did not find a great solution past some very unreadable regular expressions.

In the course of watching a movie this evening, I had a solution. I created an array of acceptable formats, looped over the array and checked each entry against the phone number. If I had a match, the validation passes. Setting the string match to the beginning and I can allow for extensions without being too persnickety about formatting.

Here it is (via a handy gist):

def valid_phone
  return true if phone.blank?

  phone_formats = [
    /^\(\d\d\d\) \d\d\d-\d\d\d\d/,

  valid = false
  phone_formats.each do |format|
    if phone.match( format )
      valid = true

  unless valid
    errors.add("Phone format isn't recognized")

Here are some spec test examples.

It doesn’t handle international formats, but, for right now, it fits my needs.

Amazon Web Services, EC2 and Windows browser testing

My wife and I are launching a website in the near future. As part of the ongoing development process, we’d been talking about testing alternate browsers. We’re able to test Internet Explorer 6 on our machines using Darwine and ie4osx. Still, that left us without a great option to do IE7 testing.

As it happens, we have an older MacBook Pro 2.33 that fell off of Craigslist. We’d like to sell it, but Robin brought up using it as a Windows machine. I figured with Boot Camp and a Windows license, sure, we could be in business. However, with Windows XP or Vista licenses running between $120 and $300, I was eager to find an alternate solution. Bootstrapping a business means funneling money into product, not blowing the budget on something we’ll use infrequently. The tool can be essential, but infrequently used and thus, too expensive.

I remembered at work, one of our sysadmins set-up up some virtual machines to allow Linux and Mac OS X users to test on Windows XP. So, my thought was to use Amazon’s Elastic Computing Cloud (EC2) running Windows Server 2003. Handily, the machine images already use IE7. I fired up EC2 instances about four different times. At $0.125 per instance/hour and a little bit extra for Amazon S3 usage to load the image, our tests wound up at a whopping $0.52.

How I went about this:

There are other places I can take this, building a machine image with other browsers installed and storing the image on S3. In fact, that’ll probably making sense for us later this week.

There are other, largely unexplored options, mentioned here for future reference:

  • Browsershots A service that screen shots a site in several browsers.
  • litmus Free testing up to 50 times/month in FF2 and IE7. Or spend $24 for a day pass or get a month for $49. The tests give you screen shots and HTML and CSS validity reports. They do email testing, too.
  • CrossBrowserTesting.com

Screen shot services aren’t as useful because while you see what the page renders as, you don’t get a sense of what quirks a user will run into if, say, a JavaScript action fires differently. That’s the situation we’re in. The pages look fine, but don’t act like they should.

← Previous Next →