All Projects → tomayac → js-input-masking

tomayac / js-input-masking

Licence: Apache-2.0 License
No description or website provided.

Projects that are alternatives of or similar to js-input-masking

input-mask
🎭 @ngneat/input-mask is an angular library that creates an input mask
Stars: ✭ 174 (+370.27%)
Mutual labels:  input-mask, inputmask
Text Mask
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Stars: ✭ 8,102 (+21797.3%)
Mutual labels:  input-mask, inputmask
react-text-mask-hoc
A higher-order text-mask component decorator for React and React Native.
Stars: ✭ 18 (-51.35%)
Mutual labels:  input-mask, inputmask
symfony-intl-fix
Provides the symfony/intl fix for new php version in symfony 2.8
Stars: ✭ 20 (-45.95%)
Mutual labels:  intl
detect-browser-language
Detect browser language
Stars: ✭ 35 (-5.41%)
Mutual labels:  intl
svelte-intl
Internationalize your Svelte apps using format-message and Intl object
Stars: ✭ 48 (+29.73%)
Mutual labels:  intl
use-input-mask
use-input-mask.now.sh
Stars: ✭ 17 (-54.05%)
Mutual labels:  input-mask
FormatEditText
格式化输入框,可用来格式化数字、金额、号码等; FormatEditText can be used as a formatted text input box
Stars: ✭ 121 (+227.03%)
Mutual labels:  input-mask
remask
A multi-mask lib
Stars: ✭ 99 (+167.57%)
Mutual labels:  input-mask
v-intl
Add i18n to your awesome Vue 3 app 🔉
Stars: ✭ 13 (-64.86%)
Mutual labels:  intl
rescript-intl
ReScript wrapper on top of JavaScript's Intl
Stars: ✭ 18 (-51.35%)
Mutual labels:  intl
vue-jquery-mask
Vue.js component for jQuery mask plugin
Stars: ✭ 19 (-48.65%)
Mutual labels:  input-mask
react-intl-currency-input
React component for i18n currency input using Intl API.
Stars: ✭ 158 (+327.03%)
Mutual labels:  intl
iXn
Control your localization of apps
Stars: ✭ 20 (-45.95%)
Mutual labels:  intl
react-theme
Production ready Wordpress theme built with React, Redux, Redux-Thunk, Intl, React Router v4, etc... and packaged by Webpack 2. Enjoy!
Stars: ✭ 14 (-62.16%)
Mutual labels:  intl
react-intl-loader
Async react-intl locale data loader for webpack
Stars: ✭ 46 (+24.32%)
Mutual labels:  intl
validarium
🛡Agnostic validation library for JavaScript applications.
Stars: ✭ 29 (-21.62%)
Mutual labels:  intl
docker-php7
A docker image with php 7 and extensions (apc, apcu, intl, mcrypt,...)
Stars: ✭ 16 (-56.76%)
Mutual labels:  intl
dogma
Things and stuffs.
Stars: ✭ 22 (-40.54%)
Mutual labels:  intl
intl-format
A wrapper library for PHP to format and internationalize values in messages like sprintf
Stars: ✭ 12 (-67.57%)
Mutual labels:  intl

Input Masking 🎭

Author: Thomas Steiner ([email protected], @tomayac)

Last updated: 2021-09-07

The problem

By "restricting input using preformatted input masks (telephone number, birthday, Social Security number), or even auto-correcting input by appending or removing unnecessary characters" [cf. Klimczak], input masking helps users enter information in forms more correctly, and existing data can be formatted adequately. Many implementations of input masking exist in user land, proving that there is a true need for this feature.

A beginning of a proposal

This proposal is to gauge interest in making input masking part of the language, maybe as a new interface of Intl. Here're some code snippets that show what this could look like in practice:

  • Globally agreed-on input mask:

    // 16 digits.
    new Intl.InputMask("credit-card-number").format("4012888888881881");
    // "4012 8888 8888 1881"
    
    // 15 digits.
    new Intl.InputMask("credit-card-number").format("378282246310005");
    // "3782 822463 10005"
  • Locale-aware input mask with customization options:

    new Intl.InputMask("phone-number", {
      locale: "de-DE",
      countryCode: "leadingPlus",
      areaCode: "leadingZero",
      groupSize: 2,
    }).format("00494012345678");
    // "+49 (0)40 12 34 56 78"
  • Fully custom input mask with a mask function:

    new Intl.InputMask("custom", {
      maskFunction: (input) => input.toLowerCase().replaceAll(" ", ""),
    }).format("No Spaces No Uppercase");
    // "nospacesnouppercase"

Polyfill

Work on a polyfill that implements this proposal has started in tomayac/js-input-masking-polyfill. You can see Intl.InputMask in action in the demo.

Isomporhic JS™

Making this part of the language would allow people to use this on the client and the server.

On the client

For example, a client-side implementation could use this as follows (note that the type="tel" of the <input> does not mean the input value is "automatically validated to a particular format before the form can be submitted, because formats for telephone numbers vary so much around the world" [cf. MDN]):

<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" />
const formatPhoneNumber = (value) => {
  return new Intl.InputMask("phone-number", {
    locale: "de-DE",
    countryCode: "leadingPlus",
    areaCode: "leadingZero",
    groupSize: 2,
  }).format(value);
};

document.querySelector("#phone").addEventListener("input", (e) => {
  e.target.value = formatPhoneNumber(e.target.value);
});

On the server

For a server-side implementation, this could look as follows:

SELECT phone FROM users_legacy;
# Returns a mix of formats from a legacy dataset:
# 00494012345678\n+494012345678\n04012345678
const formatPhoneNumber = (value) => {
  return new Intl.InputMask("phone-number", {
    locale: "de-DE",
    countryCode: "leadingPlus",
    areaCode: "leadingZero",
    groupSize: 2,
  }).format(value);
};

// Express.js YOLO example.
app.get("/phones", (req, res) => {
  const rawPhones = getPhoneNumbersFromLegacyDB();
  const formattedPhonesHTML = rawPhones
    .map((rawPhone) => {
      return formatPhoneNumber(rawPhone);
    })
    .join("<br>");
  res.send(formattedPhonesHTML);
});

Alternatives

Just leaving this to the user land is an obvious alternative. The ecosystem of input masking libraries is alive, and great implementations exist. Pulling any of those in comes at a cost for each locale that's needed though, and the weight of the particular input masking library itself.

Another alternative would be to add new (and smarter) input types. A recent example is the input[type=currency] proposal. The declarative burden for advanced formatting needs is not to be underestimated, though (see the above countryCode and areaCode examples). This also does not solve the issue on the server side.

Feedback

Feedback on this early-stage idea is welcome. Please open a new Issue.

Acknowledgements

I'm thankful for the contributions from:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].