# 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
[](https://travis-ci.org/nosir/cleave.js)
[](https://www.codacy.com/app/nosir/cleave-js)
[](https://badge.fury.io/js/cleave.js)
[](https://www.npmjs.com/package/cleave.js)
[](https://badge.fury.io/bo/cleave.js)
[](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 (