JSFiddle - React, Tailwind, and code Playground

by salitrapraveen

HTML

<input type="text" id="txtboxID" />

JavaScript

$('#txtboxID').keyup(function () {
    var val = this.value.replace(/\D/g, '');
    var newVal = '';
    while (val.length > 3) {
        if (val.length == 10) {
            newVal += val.substr(0, 4);
            return false;
        } else {
            newVal += val.substr(0, 3) + '-';
            val = val.substr(3);
        }
    }
    newVal += val;
    this.value = newVal;
});