cleave.js README

The project's README from github.com/nosir/cleave.js. As one markdown file: README.md.

⚠️ 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

Travis Codacy Badge npm version npm downloads Documents

Cleave.js has a simple purpose: to help you format input text content automatically.

Features

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

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

<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

Then have a text field

<input class="input-phone" type="text" />

Now in your JavaScript

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

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

var cleave = new Cleave(...)

AMD

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

ES Module

// 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:

import Cleave = require('cleave.js')

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

import Cleave = require('cleave.js/react')

ReactJS component usage

import React from 'react'
import ReactDOM from 'react-dom'

import Cleave from 'cleave.js/react'

Then in JSX:

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}
      />
    )
  }
}

As you can see, here you simply use <Cleave/> as a normal <input/> field

Advanced usage:

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

AngularJS directive usage

First include the directive module:

<script src="cleave.js/dist/cleave-angular.min.js"></script>
<script src="cleave.js/dist/addons/cleave-phone.{country}.js"></script>

And in your model:

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:

<div ng-controller="AppController">
  <input
    ng-model="model.rawValue"
    ng-whatever="..."
    type="text"
    placeholder="Enter credit card number"
    cleave="options.creditCard"
  />
</div>

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

Run tasks

npm install

Build assets

gulp build

Run tests

gulp test

Lint

gulp eslint

Publish (build, tests & lint)

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.

Get in touch

References

Licence

Cleave.js is licensed under the Apache License Version 2.0

Analytics

cleave.js (nosir/cleave.js)

Searching Database…

17.8K ·GitHub
这是什么?Cleave.js 是一个 JavaScript 库,可以在用户输入时自动格式化文本,例如信用卡号、电话号码、日期和数字。
它是独立的库,还是需要依赖某个框架?它是一个独立的 JavaScript 库,同时提供可选的绑定:ReactJS 组件、AngularJS(1.x)指令,以及社区支持的 VueJs 和 jQuery 用法。
怎样把它添加到项目里?可以通过 npm 安装,从 jsDelivr 或 cdnjs CDN 加载,或直接获取 dist 目录中的文件,然后在 JavaScript 中把它绑定到输入框元素上。
电话号码格式化还需要额外的文件吗?需要。电话格式化需要一个单独的按国家划分的插件文件 cleave-phone.{country}.js,因为 i18n 电话库按国家拆分,以减小打包体积。
这个项目还在维护吗?不再维护。Cleave.js 已被作者正式弃用,作者建议改用较新的 'cleave-zen' 库。
它能替代后端的输入校验吗?不能。它只能提升输入内容的可读性,并不用于替代校验或掩码逻辑,这些仍应在服务器端完成。
它开箱即用支持哪些格式?支持信用卡号格式化、电话号码格式化、日期格式化、数字格式化,以及自定义分隔符、前缀和分组模式。
有 TypeScript 类型定义吗?有。社区贡献的类型定义可通过 '@types/cleave.js' 获取,其中包括 React 组件的类型。
能同时用于多个输入框吗?可以,但每个输入框需要使用不同的 CSS 选择器,并且要为每个输入框分别创建一个 Cleave 实例,通常可以用循环来完成。
Creator
nosir
Size
3 MB
Stars
17.8K
Age
10y
Commits · 90 Days