npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phone-number-input-field

v0.1.7

Published

HTML input component that formats and validates phone numbers.

Downloads

25

Readme

Phone Number Input Field

The Phone Number Input Field is a form field with the following features:

  • Incremental Formatting
    Phone numbers are formatted gradually with each keystroke. The digits are grouped and spaced in the standard way for the country of origin.

  • Phone Number Validation
    The entered number is validated with each keystroke to determine whether it is a valid number for the country it belongs to. The standard inputElement.validity.valid property will indicate whether the phone number is valid.

  • Determination of Phone Number Type
    In many countries it is possible to distinguish between mobile and land lines, among other types of numbers.

  • Acts Like a Regular <input> Tag
    The Phone Number Input Field is an <input> tag with enhanced functionality. As a regular <input> tag, you can style it and interact with it in the normal way. It is therefore framework-agnostic, i.e. it can be used with any framework.

  • Dynamic Status Attributes
    The HTML tag adds, removes or changes the value of various attributes in order to indicate what is known so far about the number that is being entered. These can be incorporated into CSS rules that indicate the style visually.

Demonstration

See here for a live demo

Installation

Using a bundler such as Webpack, Parcel etc.

  1. Install the library into your Node project:
npm install phone-number-input-field
  1. If it is not already installed, install the libphonenumber-js peer dependency.
npm install libphonenumber-js
  1. In your HTML, refer to the libphonenumber-js and phone-number-input javascript files:
<script src="./path-to/libphonenumber-max.js"></script>
<script src="./path-to/phone-number-input.min.js"></script>

Using a CDN

In your HTML, refer to the libphonenumber-js javascript file and the phone-number-input package.

<script src="https://cdn.jsdelivr.net/npm/libphonenumber-js/bundle/libphonenumber-max.js"></script>
<script src="https://cdn.jsdelivr.net/npm/phone-number-input-field"></script>

If you want to use the source map to debug, use this instead of the second line:

<script src="https://cdn.jsdelivr.net/npm/phone-number-input-field/dist/phone-number-input.min.js"></script>

Usage

HTML

Use phone-number-input in the same way as a regular input tag, but you must specify the is="phone-number-input" attribute.

<input is="phone-number-input" />

Javascript

Create an instance, then add it to the DOM in the normal way:

// Option 1: Use createElement
const input1 = document.createElement('input', { is: 'phone-number-input' });
document.body.appendChild(input1);

// Option 2: Use the constructor
const PhoneNumberInput = customElements.get('phone-number-input');
const input2 = new PhoneNumberInput();
document.body.appendChild(input2);

Properties / Attributes

| Property | Attribute | Description | | --------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | defaultCountry | default-country | Sets/gets the default country. This is needed when the entered number does not start with a country code prefix, e.g. +44. If you have a <select> tag with a list of countries, it should set this property when a country is selected. | | errorMsg | error-msg | Sets/gets the standard HTML form validation error message that the browser will display when validating a form containing this component. Default: "Invalid phone number!" | | country | country | Gets the country of the current number. This allows the user to override the default country by using a country code prefix. | | phoneIsValid | phone-is-valid | Indicates whether the current number is considered a valid number for the country. | | phoneIsPossible | phone-is-possible | Indicates whether the length of the phone number is reasonable for the country. | | phoneType | phone-type | The type of the phone number: MOBILE, FIXED_LINE, FIXED_LINE_OR_MOBILE, PREMIUM_RATE, TOLL_FREE, SHARED_COST, VOIP, PERSONAL_NUMBER, PAGER, UAN, VOICEMAIL | | phoneE164 | phone-e164 | The phone number in E164 format, e.g. +12125551234 | | onparse | onparse | Sets an inline handler for the phone-parse event. See below. | | oncountrychange | oncountrychange | Sets an inline handler for the phone-country-change event. See below. |

Events

The following are in addition to the events dispatched by the standard <input> element.

| Event | Description | | -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | phone-parse | Dispatched whenever the value of the field is parsed. Parsing is triggered by the standard input events, but is debounced. Therefore, it may be preferable to listen to this instead of input events. | | phone-country-change | Dispatched whenever the country that is identified from the entered number changes. In many cases this is well before the number is considered valid and/or its type is known. |

Tips

  • Use the standard input field's autocomplete attribute with an appropriate value, e.g. autocomplete="tel-national" to cause the browser to suggest the user's phone number if it has been used elsewhere.

Developing & Building

  • This project uses the Hyperapp microframework, and my Hyperapp CustomElement library.
  • npm run build runs the build script.
  • npm run demo runs a local web server that will build the component and serve the demo page at http://localhost:3001/docs/