Cleave.js
Format input text content when you are typing...
README
Cleave.js
Features
Why?
Installation
npm
- ``` sh
- npm install --save cleave.js
- ```
CDN
old school
Usage
- ``` html
- <script src="cleave.min.js"></script>
- <script src="cleave-phone.{country}.js"></script>
- ```
cleave-phone.{country}.js addon is only required when phone shortcut mode is enabled. See more in documentation: phone lib addon section
- ``` html
- <input class="input-phone" type="text"/>
- ```
- ``` 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
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
- ```ts
- import Cleave = require('cleave.js');
- ```
- ```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';
- ```
- ``` js
- class MyComponent extends React.Component {
- constructor(props, context) {
- super(props, context);
- this.onCreditCardChange = this.onCreditCardChange.bind(this);
- this.onCreditCardFocus = this.onCreditCardFocus.bind(this);
- }
- onCreditCardChange(event) {
- // formatted pretty value
- console.log(event.target.value);
- // raw value
- console.log(event.target.rawValue);
- }
- onCreditCardFocus(event) {
- // update some state
- }
- render() {
- return (
- <Cleave placeholder="Enter your credit card number"
- options={{creditCard: true}}
- onFocus={this.onCreditCardFocus}
- onChange={this.onCreditCardChange} />
- );
- }
- }
- ```
AngularJS directive usage
- ``` html
- <script src="cleave.js/dist/cleave-angular.min.js"></script>
- <script src="cleave.js/dist/addons/cleave-phone.{country}.js"></script>
- ```
- ``` js
- angular.module('app', ['cleave.js'])
- .controller('AppController', function($scope) {
- $scope.onCreditCardTypeChanged = function(type) {
- $scope.model.creditCardType = type;
- };
- $scope.model = {
- rawValue: ''
- };
- $scope.options = {
- creditCard: {
- creditCard: true,
- onCreditCardTypeChanged: $scope.onCreditCardTypeChanged
- }
- };
- });
- ```
- ``` html
- <div ng-controller="AppController">
- <input ng-model="model.rawValue" ng-whatever="..." type="text" placeholder="Enter credit card number"
- cleave="options.creditCard"/>
- </div>
- ```
Use in VueJs
jQuery fn usage
Playground
Documentation
Run tasks
- ``` sh
- npm install
- ```
- ``` sh
- gulp build
- ```
- ``` sh
- gulp test
- ```
- ``` sh
- gulp eslint
- ```
- ``` sh
- gulp publish
- ```
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.