cleave.js demo

cleave.js demo

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://nosir.github.io/cleave.js/dist/cleave.min.js"></script>
<div>
  <label for="entryBox">Enter numbers: </label>
  <input type="text" class="txtEntryBox" name="entryBox" />
</div>
<div>
  <button id="btnRemoveCleave">
    Click to remove Cleave
  </button>
</div>

JavaScript

(function ($, window, undefined) {
    'use strict'

    $.fn.cleave = function (opts) {
        var defaults = { autoUnmask: false },
            options = $.extend(defaults, opts || {});

        return this.each(function () {
            var cleave = new Cleave(this, options), $this = $(this);

            $this.data('cleave-auto-unmask', options['autoUnmask']);
            $this.data('cleave', cleave);
        });
    }

    var origGetHook, origSetHook;

    if ($.valHooks.input) {
        origGetHook = $.valHooks.input.get;
        origSetHook = $.valHooks.input.set;
    } else {
        $.valHooks.input = {};
    }

    $.valHooks.input.get = function (el) {
        var $el = $(el), cleave = $el.data('cleave');

        if (cleave) {
            return $el.data('cleave-auto-unmask') ? cleave.getRawValue() : el.value;

        } else if (origGetHook) {
            return origGetHook(el);

        } else {
            return undefined;
        }
    }

    $.valHooks.input.set = function (el, val) {
        var $el = $(el), cleave = $el.data('cleave');

        if (cleave) {
            cleave.setRawValue(val);
            return $el;
        } else if (origSetHook) {
            return origSetHook(el);
        } else {
            return undefined;
        }
    }
})(jQuery, window);

var abc;

$(document).ready(function(){
	abc = $('.txtEntryBox').cleave({ numeral: true, numeralThousandsGroupStyle: 'thousand', autoUnmask: true});

	$('#btnRemoveCleave').on('click', function() {
  	console.log('destroy');
    abc.cleave.destroy();
  });
});