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

@apimediaru/masked-phone-input

v0.0.5

Published

## Installation

Downloads

9

Readme

MaskedPhoneInput

Installation

npm install @apimediaru/masked-phone-input

Basic usage

Create input element.

<input id="phone" type="tel" name="phone" data-masked-phone-input>

Import library and create new MaskedPhoneInput instances.

import { createMaskedInput, validate } from '@apimediaru/masked-phone-input';

// Bind MaskedPhoneInput instances
createMaskedInput('[data-masked-phone-input]');

const formEl = document.querySelector('form');
formEl.addEventListener('submit', (event) => {
  event.preventDefault();
  
  const phoneInputEl = form.querySelector('[name="phone"]');
  
  if (phoneInputEl && !validate(phoneInputEl)) {
    alert('Bad phone number!');
    return;
  }
  
  // Further submit logic
});

API

constructor arguments

new MaskedInput(el: HTMLInputElement, formatter: MaskedFormatter, validator: MaskedValidator): MaskedInput

Library core class, you can import it from the package, but do not use it directly with new operator, use factory instead.

import { MaskedInput } from '@apimediaru/masked-phone-input';

factory

createMaskedInput(el: HTMLInputElement | string, options?: string | MaskedLocalization): MaskedInput | MaskedInput[]

Used to create a new MaskedInput instance.

You can directly pass the HTMLInputElement and create a new instance for target input.

import { createMaskedInput } from '@apimediaru/masked-phone-input';

const instance = createMaskedInput(document.querySelector('[name="phone"]'));

Other usage is passing the CSS selector as first argument to get an array of MaskedInput instances which met the condition (elements which are not instances of HTMLInputElement are ignored).

import { createMaskedInput } from '@apimediaru/masked-phone-input';

const instances = createMaskedInput('[name="phone"]');

validate

validate(el: HTMLElement | string, locale?: string): boolean

Most of the time you want to validate HTMLInputElement, so just pass it as first argument:

import { validate } from '@apimediaru/masked-phone-input';

const element = document.querySelector('#phone');
console.log(validate(element)); // true / false

Validating elements which are not instance of HTMLInputElement will always return true.

Other case is validating directly value as a string, you can pass such value as first argument and if needed you may also specify locale (default one is used if omitted):

import { validate } from '@apimediaru/masked-phone-input';

console.log(validate('+78001234567')); // true / false

extractInstance

extractInstance(el: HTMLInputElement): IMaskedInput | null

Extract instance attached to provided HTMLInputElement.

import { extractInstance } from '@apimediaru/masked-phone-input';

const element = document.querySelector('#phone');
const instance = extractInstance(element);

extendDefaults

extendsDefaults(obj: GlobalOptions): void

Extend default options.

import { extendDefaults, Locales } from '@apimediaru/masked-phone-input';

extendDefaults({
  locale: Locales.LOCALE_GE,
});

Locales

Locales map.

import { Locales } from '@apimediaru/masked-phone-input';

console.log(Locales);

Instance methods and properties

destroy()

instance.destroy()

Destroy instance.

isValid

instance.isValid

true if value meets validator.

isInvalid

instance.isInvalid

false if value do not meets validator.

Default options

locale

Fallback locale which is used by default. Default: ru