$(document).ready(function () { $(".rupiah-format").on("keyup", function (e) { $(this).val(formatRupiah($(this).val(), "Rp")); }); function formatRupiah(number, prefix) { var numberString = number.replace(/[^,\d]/g, "").toString(), split = numberString.split(","), remainder = split[0].length % 3, rupiah = split[0].substr(0, remainder), thousands = split[0].substr(remainder).match(/\d{3}/gi); if (thousands) { separator = remainder ? "." : ""; rupiah += separator + thousands.join("."); } if (split[1] != undefined) { rupiah = rupiah + "," + split[1].substr(0, 2); } return prefix == undefined ? rupiah : rupiah ? prefix + rupiah : ""; } });