Implement your form with jquery.numeric plugin.
$(document).ready(function(){
$(".numeric").numeric();
});
Moreover this works with textareas also!
Or better change the input when user tries to submit,
$('form').submit(function () {
if ($('#numeric').value != $('#numeric').value.replace(/[^0-9\.]/g, '')) {
$('#numeric').value = $('#numeric').value.replace(/[^0-9\.]/g, '');
}
});
That's what you are doing, you want to remove the characters, other than numbers, so why make so much efforts, just remove them at the end.
And here is my favourite option,
Try the HTML5 number input:
<input type="number" value="0" min="0">
For non-compliant browsers there are Modernizr and Webforms2 fallbacks.
or
You may bind "paste" action to a function too,
$( "#input" ).on("paste", function() {
if ($('"#input').val() != $('"#input').val().replace(/[^\d\.]/g,""))
{ $('"#input').val($('"#input').val().replace(/[^\d\.]/g,""));
}
});