valid_nanp - Validate phone numbers according to the North American Numbering Plan

The project valid_nanp and its source files are hosted on GitHub.

Description

[valid_nanp] determines whether a string contains a valid phone number according to the North American Numbering Plan. Validation ignores all non-numeric characters. Additionally the number must have at least 10 digits and cannot begin with +1. Phone numbers with extensions are permitted, where any digit beyond the first 10 digits forms the extension. Optionally formats the output, substituting # for digits, and inserting any other arbitrary character.

Demo

Demo.

Usage

valid_nanp accepts two parameters.

-number is required and must be a string type. This is the phone number to be validated and optionally formatted.

-format is optional and must be a string type. This is the output format.

Examples

1
valid_nanp('631-960-7187');
1
=> true
1
valid_nanp('401-285-0696', -format='(###) ###-#### x######');
1
=> (401) 285-0696
1
valid_nanp('oogahboogah401oopsie-285-0696ext12345', -format='(###) ###-#### x######');
1
=> (401) 285-0696 x12345

Installation

The repository contains both the tag [valid_nanp] in a file named valid_nanp.lasso and a directory valid_nanp_demo containing the demo. In this directory there is a web page named index.lasso containing the valid_nanp tag and a web form.

Did you like this article? Please send me a Gratipay, as little as 25¢ per week!

Updates, suggestions and comments regarding this article may be sent to Steve Piercy, [email protected] or comment using Disqus.


Written by Steve Piercy in Lasso on Fri, Apr 23, 2010.
Last modified: Sun, Jun 1, 2014
Tags: valid_nanp, phone number, validation, Lasso

Comments

comments powered by Disqus