JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
Price (
<label>
<input type="radio" name="prop_crnt" class="currency" value="0" id="crnt-thb" required> THB</label>
<label>
<input type="radio" name="prop_crnt" class="currency" value="1" id="crnt-usd"> USD</label>):
<input type="text" name="price" class="money" size="15" maxlength="128" placeholder="1,500" required/>
JavaScript
//reset input on radio clicked
$("input[name='prop_crnt']").on('click', function() {
$('input.money').val('');
if ($('#crnt-thb').checked) {
var crncy="฿";
}else{
var crncy="$";
}
});
//change currency prefix
var format = function(num) {
var str = num.toString().replace(crncy, ""),
parts = false,
output = [],
i = 1,
formatted = null;
if (str.indexOf(".") > 0) {
parts = str.split(".");
str = parts[0];
}
str = str.split("").reverse();
for (var j = 0, len = str.length; j < len; j++) {
if (str[j] != ",") {
output.push(str[j]);
if (i % 3 == 0 && j < (len - 1)) {
output.push(",");
}
i++;
}
}
formatted = output.reverse().join("");
return (crncy + formatted + ((parts) ? "." + parts[1].substr(0, 2) : ""));
};
//currency
$(".money").keyup(function(e) {
$(this).val(format($(this).val()));
});