Forums

Remove Comma and $ with JS

Manofstyle 11 May, 2012
I have the following JS code and I'm wondering how I can add a function to strip commas and $ from the number fields. Otherwise if someone adds 10,000 or $10000 or $10,000 I get a NaN output on most of the fields. I don't have much knowledge with JS just an FYI.

$(function() {
    $('#loan_payoff_1').change(rekenen1);
    $('#loan_payoff_2').change(rekenen1);
    $('#loan_payoff_3').change(rekenen1);
    $('#loan_payoff_4').change(rekenen1);
    $('#loan_payoff_5').change(rekenen1);
    $('#loan_payoff_6').change(rekenen1);
    $('#loan_payoff_7').change(rekenen1);
    $('#loan_payoff_8').change(rekenen1);
    $('#loan_payoff_9').change(rekenen1);
    $('#loan_payoff_10').change(rekenen1);

});

function rekenen1(){
    var subtotal = Number($('#loan_payoff_1').val()) + Number($('#loan_payoff_2').val()) + Number($('#loan_payoff_3').val()) + Number($('#loan_payoff_4').val()) + Number($('#loan_payoff_5').val()) + Number($('#loan_payoff_6').val()) + Number($('#loan_payoff_7').val()) + Number($('#loan_payoff_8').val()) + Number($('#loan_payoff_9').val()) + Number($('#loan_payoff_10').val());        
    $('#subtotal').val(subtotal);
    $('#confee').val(subtotal*0.01);
    $('#flatrate').val(295);

    var grandtotal = Number($('#flatrate').val()) + Number($('#confee').val());        
    $('#grandtotal').val(grandtotal);
            
    $('#onetotal').val(grandtotal*0.38);
    $('#twototal').val(grandtotal*0.62);
};

​
Max_admin 11 May, 2012
Hi,

Server side is better but will work only after the form is submitted, you may try the code below (not tested):


$('field_id').get('value').replace(/(,|$)/g, '');


The code above should be triggered by some event, like "change" event or "submit" event or whatever you want.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Manofstyle 12 May, 2012
I found this code and it works great. Although it removes the decimal. Could you guys offer a helping hand in helping prevent this.

<script type="text/JavaScript">
function valid(f) {
!(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;
} 
</script>
</head>
<body><br>
<form id="myform" action="">
<input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
</form>
</body>
</html>
Manofstyle 15 May, 2012
Can anyone help with this?
GreyHead 17 May, 2012
Hi Manofstyle,

Do you need to have decimal places? It's tricky to clean up if the allowable format is very flexible.

Bob
This topic is locked and no more replies can be posted.