Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  57] [ 5]  / answers: 1 / hits: 17695  / 9 Years ago, fri, february 20, 2015, 12:00:00

How can I remove all text characters (not numbers or float) from a javascript variable ?



function deduct(){
var getamt= document.getElementById('cf').value; //eg: Amount is 1000
var value1 = 100;
var getamt2 = (value1-getamt);
document.getElementById('rf').value=getamt2;
}


I want getamt as number. parseInt is giving NaN result.


More From » javascript

 Answers
6

Use regular expression like this:



var getamt= document.getElementById('cf').value; //eg: Amount is 1000
var value1 = 100;
var getamt2 = value1 - getamt.replace( /D+/g, ''); // this replaces all non-number characters in the string with nothing.
console.log(getamt2);


Try this Fiddle


[#67736] Thursday, February 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;