Skip to main content

  • Login | Forgot Password?
Ribbit Developer logo

Validating USA and UK phone numbers

Submitted by san1t1 on Wed, 2010-03-10 06:46

The Ribbit API, and all the REST libraries, require you to submit phone numbers in RFC3966 format. This requires that the Ribbit US sales number (619) 916-2565 is submitted to the API as tel:16199162565

It's not always straightforward to validate phone numbers easily, ensuring that the country code is included. With the help of Javascript, we can make this a lot easier.

Here's the Javascript for the a PhoneNumber class. This class will help take a phone number in some format and attempt to translate it to the RFC3966. As with any code that relies on the use of regular expressions it may not cover all possible formats. Hopefully it's useful enough to give you a head start in building! If you want to add other countries to the validation you merely need to find and write a suitable regular expression... as well as add that to the validate function.

var PhoneNumber = function (phone, country) {
this.text=phone;
this.culture = country !== undefined ? country : "USA";
this.uri = "";
this.valid = this.validate();
return this;
};

PhoneNumber.prototype.usaRegEx=/^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
PhoneNumber.prototype.ukRegEx=/^((0))(((1[0-9]{3})|(7[1-57-9][0-9]{2}))( )?([0-9]{3}[ -]?[0-9]{3})|(2[0-9]{2}( )?[0-9]{3}[ -]?[0-9]{4}))$/;
PhoneNumber.prototype.cleanRegex=/[^0-9]/g;

PhoneNumber.prototype.validate = function(){
var str = this.text;
var valid =false;
if (this.culture === "USA" && this.usaRegEx.test(str)){
str = str.replace(this.cleanRegex,"","g");
this.uri = "tel:1" + (str.substring(0,1)=="1" ? str.substring(1) :str );
valid = true;
}
if (this.culture === "UK" && this.ukRegEx.test(str)){
str = str.replace(this.cleanRegex,"","g");
this.uri = "tel:44" + (str.substring(0,1)=="0" ? str.substring(1) :str );
valid = true;
}
return valid;
};

With that code in place, here's an example of how we use the PhoneNumber class to query the phone.uri property to get a well formatted number:

var phone = new PhoneNumber("(619) 916-2565","USA");
if (phone.valid){
alert("The RFC3966 uri is " + phone.uri);
}

With this framework in place, we hope that you find all your telephone numbers conversion woes are behind you.

  • Code
  • rest
  • REST API
  • ribbit
  • ribbit API
  • san1t1's blog
  • Login or register to post comments
  • Share/Save

Ribbit Photos

Ribbit's Webinar SeriesTeam Ribbit Takes the Livestrong ChallengeTeam Ribbit Livestrong UpdateNEW: Ribbit API PricingCloudforce: San Jose 2010Cloudforce: San Jose 2010

See more photos at Flickr

Ribbit Conversations

  • HTML5. Flash. Silverlight. Oh My. When languages matter, and when they don't. Booyah. http://su.pr/1yB5zD /via @san1t1 — 2 days 12 hours ago
  • Honestly, it doesn't get much better than a little Massive Attack gliding over the airwaves on a Monday afternoon... — 2 days 13 hours ago
  • @zacharye: We are working on ways to better integrate the phone, email and the desktop together. Great feedback, thank you. — 2 days 13 hours ago
  • @bugdave: Sending your note about g.722 voice codec to the fellas who can make it happen. Keep your fingers crossed. :) — 2 days 13 hours ago
  • @ldrewery: Seems @guinnsquirrel hit a snag and needs your help getting your Android client to work/authenticate? — 1 week 1 day ago
  •  
  • 1 of 101
  • ››
more

Follow Ribbit

 

            

            

           

Categories

  • .NET
  • AIR
  • Applications
  • Articles and Media Mentions
  • Challenge
  • Code
  • Community
  • Contest
  • Design
  • Events
  • Feature
  • Flash
  • Flash Toolkit
  • Flex
  • Flex SDK
  • Gallery
  • General
  • Inside Ribbit
  • Interviews
  • News
  • Newsletter
  • PHP
  • REST
  • Role Playing
  • Silverlight
  • Tips & Tricks
  • Uncategorized
  • Wish List

Archives

  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • November 1999

 

               

               

  • Company
    • Corporate Site
    • About Us
    • Careers
    • Contact Us
    • LegalPrivacy
    • News
    • Media Kit
  • Products
    • Platform
    • Mobile
    • Salesforce
    • Oracle
  • Solutions
    • Digital Agencies
    • Carriers
    • Systems Integrators
    • Hosted Contact Centers
  • Community
    • Corporate Blog
    • Developer Blog
    • CRM Blog
    • Moble Blog
    • Idea Wall
    • Events Calendar
  • Support
    • Developer Help
    • Ribbit for Salesforce Help
    • Ribbit for Oracle Help
    • Ribbit Mobile Help
    • Feedback
    • Developer Forums
    • Ribbit Mobile Forum
  • Developers
    • Developer Center
    • Develop for Ribbit Mobile
    • Register
    • Ribbit Labs

© 2010 Ribbit Corporation. All Rights Reserved.