# cleave.js **Repository Path**: edik/cleave.js ## Basic Information - **Project Name**: cleave.js - **Description**: Cleave是一个JavaScript库,可以在输入框中自动对值设定格式。例如,它可以在货币输入框中添加千位分隔符、在引用卡号码输入框中自动进行号码分割、在日期输入框中自动添加“/”等。使用这个库可以让你的网站拥有更好的使用体验。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: http://nosir.github.io/cleave.js/ - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-08-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Cleave.js [![Travis](https://img.shields.io/travis/nosir/cleave.js.svg?maxAge=2592000)](https://travis-ci.org/nosir/cleave.js) [![Codacy branch grade](https://img.shields.io/codacy/grade/b1c0b0da42fa418f887076a3f7352aea/master.svg?maxAge=2592000)](https://www.codacy.com/app/nosir/cleave-js) [![npm version](https://badge.fury.io/js/cleave.js.svg)](https://badge.fury.io/js/cleave.js) [![npm downloads](https://img.shields.io/npm/dm/cleave.js.svg)](https://www.npmjs.com/package/cleave.js) [![Bower version](https://badge.fury.io/bo/cleave.js.svg)](https://badge.fury.io/bo/cleave.js) [![Documents](https://img.shields.io/badge/documents-check-3362c2.svg)](https://github.com/nosir/cleave.js/blob/master/doc/doc.md) 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) **TL;DR** [the demo page](http://nosir.github.io/cleave.js/) ## 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 ``` #### bower ```bash bower install --save cleave.js ``` #### old school Grab file from [dist](https://github.com/nosir/cleave.js/tree/master/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](https://github.com/nosir/cleave.js/blob/master/doc/phone-lib-addon.md) section Then have a text field ```html ``` Now in your JavaScript ```js var cleave = new Cleave('.input-phone', { phone: true, phoneRegionCode: '{country}' }); ``` More examples: [the demo page](http://nosir.github.io/cleave.js/) #### CommonJS ```js var Cleave = require('cleave.js'); require('cleave.js/dist/addons/cleave-phone.{country}'); var cleave = new Cleave(...) ``` For **Browserify** users, please also run this: ```bash npm install --save babelify browserify-shim ``` And add this empty `browserify-shim` config to your package.json: ```js "browserify-shim": { } ``` #### AMD ```js require(['cleave.js/dist/cleave.min', 'cleave.js/dist/addons/cleave-phone.{country}'], function (Cleave) { var cleave = new Cleave(...) }); ``` ## 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.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 ( ); } } ``` 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 Usage for `Webpack`, `Browserify` and more in documentation: [ReactJS component usage](https://github.com/nosir/cleave.js/blob/master/doc/reactjs-component-usage.md) ## AngularJS directive usage First include the directive module: ```html ``` And in your model: ```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 } }; }); ``` Then easily you can apply `cleave` directive to `input` field: ```html
``` More usage in documentation: [Angular directive usage](https://github.com/nosir/cleave.js/blob/master/doc/angularjs-directive-usage.md) ## Playground - [Plain JSFiddle (Basic usage)](https://jsfiddle.net/nosir/kbaxx64s/) - [Plain JSFiddle (More examples)](https://jsfiddle.net/nosir/aLnhdf3z/) - [React JSFiddle](https://jsfiddle.net/nosir/gLLsrxxf/) - [Angular JSFiddle](https://jsfiddle.net/nosir/q58sh22t/) ## Documentation - [JavaScript API](https://github.com/nosir/cleave.js/blob/master/doc/js-api.md) - [Constructor](https://github.com/nosir/cleave.js/blob/master/doc/constructor.md) - [Options](https://github.com/nosir/cleave.js/blob/master/doc/options.md) - [Public methods](https://github.com/nosir/cleave.js/blob/master/doc/public-methods.md) - [Phone lib addon](https://github.com/nosir/cleave.js/blob/master/doc/phone-lib-addon.md) - [ReactJS component usage](https://github.com/nosir/cleave.js/blob/master/doc/reactjs-component-usage.md) - [AngularJS directive usage](https://github.com/nosir/cleave.js/blob/master/doc/angularjs-directive-usage.md) ## Run tasks ```bash npm install ``` Build assets ```bash gulp build ``` Run tests ```bash gulp test ``` Lint ```bash gulp eslint ``` Publish (build, tests & lint) ```bash gulp publish ``` ## Todo - [x] ReactJS component - [x] Add credit card type detection callback - [ ] Fix the classic cursor jumping issue - [x] AngularJS directive (1.x) - [x] Unit tests for formatter - [x] PhantomJS browser tests - [ ] ReactJS / AngularJS browser tests > For contributors, please run `gulp publish` to ensure your PR passes tests and lint, also we have a [not in the plan](https://github.com/nosir/cleave.js/blob/master/doc/not-in-the-plan.md) list you may concern. ## Get in touch - Bugs / Suggestions: [open an issue](https://github.com/nosir/cleave.js/issues) - Twitter: [@rison](https://twitter.com/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](http://www.apache.org/licenses/LICENSE-2.0) - Google [libphonenumber](https://github.com/googlei18n/libphonenumber) is included under its [Apache License Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) [![Analytics](https://ga-beacon.appspot.com/UA-79828599-1/cleave.js?pixel)](https://github.com/igrigorik/ga-beacon)