I would like to remove all non-numeric characters from the user's input in an <input type="number"> element, but I cannot get this to work. I have code that works fine on an <input type="text"> element, but the exact same code is not working when you change text to number.
HTML:
<input type="text">
<input type="number">
jQuery:
// Works
$("[type='text']").on("change",function(){
var x = $(this).val();
x = parseInt(x.replace(/\D/g,''),10);
if (isNaN(x)) {x = "";}
$(this).val(x);
});
// Doesn't work
$("[type='number']").on("change",function(){
var x = $(this).val();
x = parseInt(x.replace(/\D/g,''),10);
if (isNaN(x)) {x = "";}
$(this).val(x);
});
How do I get this to work?
Update: @axlpl's answer below got us to what we needed. I used his code as a starting point and modified it to make sure it worked with Apple's command key and also to allow certain other things to be done in the cell. Fiddle.