Cleave.js

Format input text content when you are typing...

README

Cleave.js

Travis Codacy Badge npm version npm downloads Documents

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


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


  1. ``` sh
  2. npm install --save cleave.js
  3. ```

CDN


cleave.js is available on jsDelivr and on cdnjs.com

old school

Grab file from dist directory

Usage


Simply include

  1. ``` html
  2. <script src="cleave.min.js"></script>
  3. <script src="cleave-phone.{country}.js"></script>
  4. ```

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

  1. ``` html
  2. <input class="input-phone" type="text"/>
  3. ```

Now in your JavaScript

  1. ``` js
  2. var cleave = new Cleave('.input-phone', {
  3.     phone: true,
  4.     phoneRegionCode: '{country}'
  5. });
  6. ```

.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

  1. ``` js
  2. var Cleave = require('cleave.js');
  3. require('cleave.js/dist/addons/cleave-phone.{country}');

  4. var cleave = new Cleave(...)
  5. ```

AMD


  1. ``` js
  2. require(['cleave.js/dist/cleave.min', 'cleave.js/dist/addons/cleave-phone.{country}'], function (Cleave) {
  3.     var cleave = new Cleave(...)
  4. });
  5. ```

ES Module

  1. ``` js
  2. // Rollup, WebPack
  3. import Cleave from 'cleave.js';
  4. var cleave = new Cleave(...)

  5. // Browser
  6. import Cleave from 'node_modules/cleave.js/dist/cleave-esm.min.js';
  7. var cleave = new Cleave(...)
  8. ```

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:

  1. ```ts
  2. import Cleave = require('cleave.js');
  3. ```

Types for the React-component are also available and can be imported in the same way.

  1. ```ts
  2. import Cleave = require('cleave.js/react');
  3. ```

ReactJS component usage


  1. ``` js
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';

  4. import Cleave from 'cleave.js/react';
  5. ```

Then in JSX:

  1. ``` js
  2. class MyComponent extends React.Component {

  3.     constructor(props, context) {
  4.         super(props, context);
  5.         this.onCreditCardChange = this.onCreditCardChange.bind(this);
  6.         this.onCreditCardFocus = this.onCreditCardFocus.bind(this);
  7.     }

  8.     onCreditCardChange(event) {
  9.         // formatted pretty value
  10.         console.log(event.target.value);

  11.         // raw value
  12.         console.log(event.target.rawValue);
  13.     }

  14.     onCreditCardFocus(event) {
  15.         // update some state
  16.     }

  17.     render() {
  18.         return (
  19.             <Cleave placeholder="Enter your credit card number"
  20.                 options={{creditCard: true}}
  21.                 onFocus={this.onCreditCardFocus}
  22.                 onChange={this.onCreditCardChange} />
  23.         );
  24.     }
  25. }
  26. ```

As you can see, here you simply use `` as a normal `` field

- Attach HTML `` attributes
- Pass in the custom options prop
- Add ReactJS onChange event listener

Advanced usage:


Usage for Webpack, Browserify and more in documentation: ReactJS component usage

AngularJS directive usage


First include the directive module:

  1. ``` html
  2. <script src="cleave.js/dist/cleave-angular.min.js"></script>
  3. <script src="cleave.js/dist/addons/cleave-phone.{country}.js"></script>
  4. ```

And in your model:

  1. ``` js
  2. angular.module('app', ['cleave.js'])

  3. .controller('AppController', function($scope) {
  4.     $scope.onCreditCardTypeChanged = function(type) {
  5.         $scope.model.creditCardType = type;
  6.     };

  7.     $scope.model = {
  8.         rawValue: ''
  9.     };

  10.     $scope.options = {
  11.         creditCard: {
  12.             creditCard: true,
  13.             onCreditCardTypeChanged: $scope.onCreditCardTypeChanged
  14.         }
  15.     };
  16. });
  17. ```

Then easily you can apply cleave directive to input field:

  1. ``` html
  2. <div ng-controller="AppController">
  3.     <input ng-model="model.rawValue" ng-whatever="..." type="text" placeholder="Enter credit card number"
  4.         cleave="options.creditCard"/>
  5. </div>
  6. ```

More usage in documentation: Angular directive usage

Use in VueJs

While this package does not have an official support for use in VueJs. This can be done in few simple steps.
Please check here

jQuery fn usage

Please check here

Playground



Documentation


    - Constructor
    - Options
    - Public methods

Run tasks


  1. ``` sh
  2. npm install
  3. ```

Build assets

  1. ``` sh
  2. gulp build
  3. ```

Run tests

  1. ``` sh
  2. gulp test
  3. ```

Lint

  1. ``` sh
  2. gulp eslint
  3. ```

Publish (build, tests & lint)

  1. ``` sh
  2. gulp publish
  3. ```

For contributors, please run gulp publish to ensure your PR passes tests and lint, also we have a not in the plan list you may concern.


Get in touch

- Twitter: @rison

References


- Payment credit card number IIN https://en.wikipedia.org/wiki/Payment_card_number#Issuer_identification_number_.28IIN.29
- Google phone numbers formatting https://github.com/googlei18n/libphonenumber
- Decimal mark and thousands separating style https://en.wikipedia.org/wiki/Decimal_mark#Examples_of_use

Licence


Cleave.js is licensed under the Apache License Version 2.0

- Google libphonenumber is included under its Apache License Version 2.0
Analytics