javascript - Removing everything except numbers in a string -
i've made small calculator in javascript users enter interest rate , amount want borrow, , calculates how of incentive might get.
the problem i'm worried users enter symbols, e.g.
loan amount: £360,000 - interest rate: 4.6%
i'm not worried decimal places these needed , don't seem affect calculation, it's symbols £ , % mess things up.
is there simple way strip out these symbols code:
<td><input type="text" name="loan_amt" style="background-color:#fff380;"></td> <td><input type="text" name="interest_rate" style="background-color:#fff380;"></td> function calculate() { var loan_amt = document.getelementbyid('loan_amt').value; //this how loan amount entered var interest_rate = document.getelementbyid('interest_rate').value; //this how interest rate alert (interest_rate); }
note should use correct dom id refer via getelementbyid; can use .replace() method that:
var loan_amt = document.getelementbyid('loan_amt'); loan_amt.value = loan_amt.value.replace(/[^0-9]/, '');
but remove float point delimiter too. think answer question, not solution of problem. parse user input number, can use parsefloat() - think more correct.
Comments
Post a Comment