JSFiddle - React, Tailwind, and code Playground
by Sean Cannon
HTML
Type something
<input type="text" id="foo" />
JavaScript
String.prototype.splice = function( idx, rem, s ) {
return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
};
String.prototype.addDash = function () {
return this.splice( -3, 0, '-' );
};
foo.addEventListener('keyup', function (e) {
if (this.value.length >= 4) {
this.value = this.value.replace(/\-/gi, '').addDash();
}
});