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
var format = function(num){
var cur = $('#crnt-thb').is(":checked") ? "฿" : "$", str = num.toString().replace("฿", "").replace("$", ""), 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("").toString();
return(cur + formatted + ((parts) ? "." + parts[1].substr(0, 2) : ""));
};
//currency
$(".money").keyup(function(e) {
$(this).val(format($(this).val()));
});
$("input[name='prop_crnt']").on('click', function(){
$(".money").keyup();
});