I'm learning JavaScript and trying to format my form's input text box to currency when updated. I'm using the below code, but it does not seem to work. Can someone help me out?
<input type="text" name="amount" onChange="currencyFormat(this.value, 'amount')" class="amount">
<script type="text/javascript">
function currencyFormat(num, fldname) {
document.getElementsByName(fldname).value = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}
</script>