nosir /cleave.js
Format input text content when you are typing...
★ 17,835⑂ 1,557👁 177⑃ 204● 516▤ 66
+0
Daily
17,835
stars
1,557
forks
177
watchers
204
prs
516
issues
66
releases
README
⚠️ Deprecated Message
Message: Kindly note that this library has been deprecated. If you are still interested in using Cleave, please explore the new version available at: cleave-zen.
And this is the message from author: https://github.com/nosir/cleave.js/issues/723
Cleave.js
Cleave.js has a simple purpose: to help you format input text content automatically.
Features
- Credit card number formatting
- Phone number formatting (i18n js lib separated for each country to reduce size)
- Date formatting
- Numeral formatting
- Custom delimiter, prefix and blocks pattern
- CommonJS / AMD mode
- ReactJS component
- AngularJS directive (1.x)
- ES Module
TL;DR the demo page
Why?
The idea is to provide an easy way to increase input field readability by formatting your typed data. By using this library, you won't need to write any mind-blowing regular expressions or mask patterns to format input text.
However, this isn't meant to replace any validation or mask library, you should still sanitize and validate your data in backend.
Installation
npm
bash
npm install --save cleave.js
CDN
cleave.js is available on jsDelivr and on cdnjs.com
old school
Grab file from dist directory
Usage
Simply include
html
cleave-phone.{country}.js addon is only required when phone shortcut mode is enabled. See more in documentation: phone lib addon section
Then have a text field
html
Now in your JavaScript
js
var cleave = new Cleave('.input-phone', {
phone: true,
phoneRegionCode: '{country}',
})
.input-element here is a unique DOM element. If you want to apply Cleave for multiple elements, you need to give different CSS selectors and apply to each of them, effectively, you might want to create individual instance by a loop, e.g. loop solution
More examples: the demo page
CommonJS
js
var Cleave = require('cleave.js');
require('cleave.js/dist/addons/cleave-phone.{country}');
var cleave = new Cleave(...)
AMD
js
require(['cleave.js/dist/cleave.min', 'cleave.js/dist/addons/cleave-phone.{country}'], function (Cleave) {
var cleave = new Cleave(...)
});
ES Module
js
// Rollup, WebPack
import Cleave from 'cleave.js';
var cleave = new Cleave(...)
// Browser
import Cleave from 'node_modules/cleave.js/dist/cleave-esm.min.js';
var cleave = new Cleave(...)
TypeScript
Types are contributed by the community and are available via npm install --save-dev @types/cleave.js. Once installed, you can import Cleave like the following:
ts
import Cleave = require('cleave.js')
Types for the React-component are also available and can be imported in the same way.
ts
import Cleave = require('cleave.js/react')
ReactJS component usage
js
import React from 'react'
import ReactDOM from 'react-dom'
import Cleave from 'cleave.js/react'
Then in JSX:
js
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context)
this.onCreditCardChange = this.onCreditCardChange.bind(this)
this.onCreditTop issues
#412US Phone Number with extension12 comments · 2018-10-26#601undefined is not an object (evaluating 'e.slice') 11 comments · 2020-06-17#195Samsung Android Chrome: Problems with number input10 comments · 2017-06-12#459Cleave is broken with Vue8 comments · 2019-04-04#451Too eager date formatting8 comments · 2019-03-25#608cleave.min.js:8 Uncaught TypeError: Cannot read property 'inputType' of undefined6 comments · 2020-07-12#450How to run instance.getRawValue(), please?6 comments · 2019-03-24#141Error in react-native: Expected a component class, got [object Object]6 comments · 2016-12-08
Latest release
Fix mobile keyboard issue and noImmediatePrefix broken issue v1.6.0 2020-05-19https://github.com/nosir/cleave.js/pull/588 https://github.com/nosir/cleave.js/pull/582 https://github.com/nosir/cleave.js/pull/578