JSFiddle - React, Tailwind, and code Playground
by Ozren Tkalčec Krznarić
HTML
<form>
<input type="text" id="test01" /><br />
<input type="text" id="xlLike" value="1234567.89" /><br />
<input type="text" id="xlLike2" value="23897123423.12093" /><br />
<button type="submit" title="Submit">Submit</button>
</form>
JavaScript
(function ($) {
$.fn.excelify = function (options) {
var settings = $.extend({
markAttr: "data-excelify",
toNumber: false,
force: false
}, options);
if (settings.toNumber) {
this.each(function () {
$(this).val(getNumberFormat($(this).val()));
});
} else {
if (settings.force) {
this.each(function () {
$(this).val(getDecoratedFormat($(this).val()));
});
}
this.each(function () {
$this = $(this);
$this
.attr(settings.markAttr, true)
.keypress(function (e) {
return /[0-9\,\.]/.test(String.fromCharCode(e.which));
})
.focus(function () {
$this = $(this);
$this.val(getNumberFormat($this.val()));
})
.blur(function () {
$this = $(this);
$this.val(getDecoratedFormat($this.val()));
})
})
}
return this;
};
function getNumberFormat(text) {
return text.replace(/[\,]/gi, '');
};
function getDecoratedFormat(text) {
var v = text.replace(/[\,]/gi, '');
if (v == '')
return v;
var tokens = v.split('.');
var fract = tokens.length > 1 ? parseInt(tokens.pop()) : 0;
var integ = tokens.length > 0 ? parseInt(tokens.pop()) : 0;
if (isNaN(fract) || isNaN(integ))
return '';
var thsep =
(integ + '')
.split('')
.reverse()
.join('')
.match(/.{1,3}/g)
.join(',')
.split('')
.reverse()
.join('');
return thsep + "." + fract;
};
})(jQuery);
$('#xlLike').excelify({force:...