nosir / cleave.js
- среда, 13 июля 2016 г. в 03:12:49
JavaScript
Format input text content when you are typing
Cleave.js has a simple purpose: to help you format input text content automatically.
TL;DR the demo page
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.
npm install --save cleave.js
bower install --save cleave.js
Grab file from dist directory
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
var Cleave = require('cleave.js');
require('cleave.js/dist/addons/cleave-phone.{country}');
var cleave = new Cleave(...)
require(['cleave.js/dist/cleave.min', 'cleave.js/dist/addons/cleave-phone.{country}'], function (Cleave) {
var cleave = new Cleave(...)
});
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);
}
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} />
);
}
}
As you can see, here you simply use <Cleave/>
as a normal <input/>
field
<input/>
attributesoptions
proponChange
event listenerSee more in documentation: ReactJS component usage section
npm install
Build assets
gulp build
Run unit tests and lint
gulp mocha && gulp eslint
Cleave.js is licensed under the Apache License Version 2.0