Input输入时,表单实时格式化:如手机号码,身份号,信用卡,银行卡号

Format your content when you are typing
在线案例 https://nosir.github.io/cleave.js/
https://github.com/nosir/cleave.js

  1. npm install cleave.js

使用

  1. // Rollup, WebPack
  2. import Cleave from 'cleave.js';
  3. var cleave = new Cleave('.input-phone', {
  4. phone: true,
  5. phoneRegionCode: '{country}'
  6. });

react

  1. import React from 'react';
  2. import Cleave from 'cleave.js/react';
  3. function App() {
  4. onCreditCardChange(event) {
  5. // formatted pretty value
  6. console.log(event.target.value);
  7. // raw value
  8. console.log(event.target.rawValue);
  9. }
  10. onCreditCardFocus(event) {
  11. // update some state
  12. }
  13. const attrs = {
  14. placeholder: '请输入银行卡号',
  15. options: { creditCard: true },
  16. onFocus: onCreditCardFocus,
  17. onChange: onCreditCardChange,
  18. };
  19. return (
  20. <Cleave {...attrs} />
  21. );
  22. }

数字格式化案例

银行卡格式化

image.png

日期格式化

image.png