This is my current code it removes all non-numeric characters except $, commas and dots from an input when as user types:
<input type="text" id="price" name="price" onkeyup="updatePrice(this.value)">
function updatePrice(p) {
document.getElementById("price").value = p.replace(/[^0-9$.,]/g, '');
}
The problem is that it removes characters after typing them so if you type A you see it for a fraction of a second before it disappears. Keydown is no good since it runs the script before the input actually changes.
How can I completely prevent these forbidden characters from appearing on the input?
onblurto perform validation when the input loses its focus – the user dosen’t have to be aware of this during typing.