Cleave.js: Support multiple delimiters

Created on 14 Jul 2016  路  11Comments  路  Source: nosir/cleave.js

Today there is only support for a single delimiter, so some more complex patterns can't be done.

Some examples:

  • 999.999.999-99 BR CPF (like US social security for BR people)
  • (99)99999-9999 BR phone

My proposal to fix that is add delimiters option that would take a array of delimiters, each to be used one time in the rigth order between blocks. This wouldn't add breaking changes. Maybe some validations should be applied like not allowing both delimiter and delimiters at the same time, and delimiters.length === blocks.length - 1.

The options passed to a CPF input would be

{
  blocks: [3,3,3,2], 
  delimiters: ['.', '.', '-'], 
  numericOnly: true
}
enhancement resolved

Most helpful comment

Release published. Please see: https://github.com/nosir/cleave.js/blob/master/doc/options.md#delimiters
Won't change blocks usage, that will confuse other users.

A demo is added to homepage: http://nosir.github.io/cleave.js/

Cheers.

All 11 comments

@vicentedealencar +1 to support this feature, for Brazil will be an awesome feature.

That's will be great. WE haven't good libs that support brazilian formats. +1

Another way would be to supercharge the blocks option:

{
  blocks: [3, '.', 3, '.', 3, '-', 2]
}

Which would be nice for other uses as well:

{
  blocks: [3, '.', 2, '%']
}

@gibatronic I liked your idea, but this change will have a good impact in the library.

yea @renatoagds... it would be hard to avoid weird glitches. :pensive:

I've submitted a suggestion for an IdFormatter, if anyone could help it would be great!

Amazing @gibatronic.. I am going to look that today, maybe I could help w/ something.

Sorry guys, I've closed the Id card formatter PR. Id card number is a specific requirement, we won't merge formatter for a relatively specific feature to the main repo.

Besides, multiple delimiters option is something we can add, @nosir is looking at that and will add soon.

Cheers.

Release published. Please see: https://github.com/nosir/cleave.js/blob/master/doc/options.md#delimiters
Won't change blocks usage, that will confuse other users.

A demo is added to homepage: http://nosir.github.io/cleave.js/

Cheers.

Awesome, dude!

One thing, it looks like there is a missing s on the example delimiter: ['.', '.', '-'].

Oops, will fix it at night. Good catch!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArtyomBist picture ArtyomBist  路  6Comments

wcedmisten picture wcedmisten  路  4Comments

YanikCeulemans picture YanikCeulemans  路  3Comments

periabyte picture periabyte  路  7Comments

CaptainYarb picture CaptainYarb  路  3Comments