I’m using the following code to clean input fields whose class contains .number. I need the value to contain only numbers, no commas, dots, or anything but numbers:
var numberEle = $(form).find('.number'); // check number fields
for(var i=0;i<numberEle.length;i++){
var numValue = $(numberEle[i]).val()
var numValue = numValue.replace(/\s/g,""); // removes spaces
var numValue = numValue.replace(/\D/g,""); // leaves only numbers
$(numberEle[i]).val(numValue);
}
The above code works fine, but for several reasons it looks to me like it is not properly written, meaning that it could be much “cleaner” and simpler, any help?
This should do it:
http://jsfiddle.net/4hrqq/