JavaScript Test For Valid US Phone Number


Example and script at

In building an app, I needed to check for a valid phone number based on user input. After reading and learning more about US phone numbers than I cared about, I was able to come up with a basic way to check for a valid US phone number using JavaScript. It only marks a phone number as invalid if it is a phone number that is not a possible phone number or a special phone number that customers will not have (800 numbers, etc.).

I built a US phone number validation test page that will test phone numbers you input and give the reason why it fails if it doesn’t pass validation. You can check it out at

Resources I ended up using in my research or the formulation of the algorithm:


Photo by Paul L

JSONP jQuery Ajax Error Handler Callback

Are you using JSONP and jQuery and trying to handle non 200 responses? Getting frustrated that your error handler isn’t firing when it gets a 403, 404, or some other response code? Take a breath and repeat after me.

“There is no error handling in JSONP”

Now that you have fallen into a great despair, let me save you.

Head over to to find a good alternative to jQuery’s implementation of JSONP.

The solution was easy to implement and helped me solve the issues I was having with getting 403 and 404 errors on JSONP requests.

jQuery-JSONP is feature rich with

  • error recovery in case of network failure or ill-formed JSON responses,
  • precise control over callback naming and how it is transmitted in the URL,
  • multiple requests with the same callback name running concurrently,
  • two caching mechanisms (browser-based and page based),
  • the possibility to manually abort the request just like any other AJAX request,
  • timeout mechanism.

and compatible with all major browsers

  • Internet Explorer 6+
  • Firefox 2+
  • Chrome 1+
  • Safari 3+
  • Opera 9+

Photo by janet galore

Preventing console.log Undefined Errors

I don’t think it’s a good idea to leave


statements in production code, it’s a good idea to prevent your site from breaking if you accidentally do. I include this condition in the first script that loads on the page of most of my projects

if (typeof console == "undefined") {
    window.console = {
        log: function () {}

This JavaScript simply defines an empty function if the console is not available in the browser so stray log statements don’t break production for your valuable customers.