function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
This code works by allowing only a numeric value to be entered, but I need to be able to add something like 2.5 as well. So can it be adjusted so a . can be entered?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
This is restricting the actual key codes, which seems like a strange approach. On my keyboard, period is 110 and decimal (on the numpad) is 190 if you want to continue down that road.
Google "javascript verify if string is numeric" to get some ideas. You should verify the value after input anyway.
And, if you are going to check keypresses, then you may need to track whether the user type '.' twice. You need to only allow '.NN', 'NN.NN' and 'NN.' , not something like '.NN.NN.'
Very good point. They are unlikely to do that. It's mainly so they don't enter 2.5", but only enter 2.5. So if that was the only real concern, how could i adjust my code so that it takes 0-9 and . ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
The bigger point here is to validate the data they have entered rather than trying to restrict what they can enter. 222.555.22.5.25 is no more valid than 2.5"