Removing everything except numbers in a string

前端 未结 6 951
旧巷少年郎
旧巷少年郎 2020-12-11 14:16

I\'ve made a small calculator in javascript where users enter the interest rate and amount the they want to borrow, and it calculates how much of an incentive they might get

6条回答
  •  长情又很酷
    2020-12-11 15:13

    Note that you should use the correct DOM id to refer via getElementById. You can use the .replace() method for that:

    var loan_amt = document.getElementById('loan_amt');
    loan_amt.value = loan_amt.value.replace(/[^0-9]/g, '');
    

    But that will remove float point delimiter too. This is an answer to your question, but not a solution for your problem. To parse the user input as a number, you can use parseFloat() - I think that it will be more appropriate.

提交回复
热议问题