I require some sort of direction with how to only allow either an int or float to be entered into a form input field. The validation must happen on the key up event. The problem I am having is that when entering, for example, 1.2 the check within the keyup event function sees 1. which is not a number.
Here is the code I have:
document.id('inputheight').addEvent('keyup', function(e) {
this.value = this.value.toFloat();
if (this.value == 'NaN') {
this.value = 0;
}
});
Any help is appreciated!
You could simply clean up the value of the field on keyup. Something like this should do the trick:
The regular expression instantly replaces the value with the first numeric string it encounters.