Package Exports
- cleave.js
- cleave.js/dist/addons/cleave-phone.au.js
- cleave.js/dist/addons/cleave-phone.br
- cleave.js/dist/addons/cleave-phone.ca
- cleave.js/dist/addons/cleave-phone.gb
- cleave.js/dist/addons/cleave-phone.i18n
- cleave.js/dist/addons/cleave-phone.i18n.js
- cleave.js/dist/addons/cleave-phone.kz
- cleave.js/dist/addons/cleave-phone.no.js
- cleave.js/dist/addons/cleave-phone.ru
- cleave.js/dist/addons/cleave-phone.us
- cleave.js/dist/addons/cleave-phone.us.js
- cleave.js/dist/cleave
- cleave.js/dist/cleave-react
- cleave.js/dist/cleave.js
- cleave.js/react
- cleave.js/src/common/DefaultProperties
- cleave.js/src/shortcuts/CreditCardDetector
- cleave.js/src/shortcuts/DateFormatter
- cleave.js/src/shortcuts/NumeralFormatter
- cleave.js/src/shortcuts/PhoneFormatter
- cleave.js/src/utils/Util
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cleave.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Cleave.js
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 by country to reduce size)
- Date formatting
- Numeral formatting
- Custom delimiter, prefix and blocks pattern
- CommonJS / AMD mode
- ReactJS component port
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
bower
bower install --save cleave.js
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,
regionCode: '{country}'
});
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(...)
});
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 {
onCreditCardChange(event) {
// formatted pretty value
console.log(event.target.value);
// raw value
console.log(event.target.rawValue);
}
render() {
return (
<Cleave placeholder="Enter your credit card number"
options={{creditCard: true}}
onChange={this.onCreditCardChange.bind(this)} />
);
}
}
As you can see, here you simply use <Cleave/>
as a normal <input/>
field
- Attach HTML
<input/>
attributes - Pass in the custom
options
prop - Add ReactJS
onChange
event listener
See more in documentation: ReactJS component usage section
Playground
Documentation
Building & Running tests
npm install
Build assets
gulp build
Run unit tests and lint
gulp mocha && gulp eslint
Todo
- ReactJS component port
- Mocha unit tests for formatter classes
- AngularJS component port
- PhantomJS / Jest browser tests
Get in touch
- Bugs / Suggestions: open an issue
- 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