Telephone forms input data validation regular expression script

Telephone REGEX for forms data validation

Description

This script will format an input of type="text" or type="tel" into XXX-XXX-XXXX. It listens on the click and keyup events handler for validating the user entered forms data. It filters data allowing only digits data and arrow right/left, delete and backspace key events. It filters and validates the data using a regular expression formatted for a standard telephone number. The format includes the area code but does not the international code.

Audience

You are a web designer who knows how to integrate a plugin into a web page; namely you know html, css and javascript.

Usage

Add the script to your page. Set the classname for all inputs elements that you wish to have formatted by the script. Pass the classname you set, as string to the setup function named setupTIFs("classname_string"). For example, if you set class="tel_input" then your JavaScript would simply be as follows:

setupTIFs("tel_input");

Demo

js file link: ./Tel_inp_formatter.js

Final Notes

This script works on all modern browsers including IE9 and above.