Bad idea! Better validate the input later, otherwise your validation per key press will create a great confusion in the user.
Here is why. Imagine your max value is 900. The user enters 799 (valid) and realized that the valid value would be 789 (still valid). The user can delete on of '9' and enter '8'. It would keep input valid all the time. Alternatively, the user can enter '8' after already entered '7' in hope to delete extra '9' on next step. To the user surprise, an error message will be shown. The user will have to click "OK" (or something) and delete extra '9' after that, if it's even possible, probably mumbling some 4-letter words at your address in the act.
Do you want it?
Better validate the input later, at the moment it is used.
At the same time, filtering out of unwanted characters on input would be quite useful. You can allow only digits, for example. Don't forget to allow the character #8, backspace — it is treated like a character, probably by historical reasons.
For example of such code, see
how to restrict users to put the numbers in range of -4713 and +9999 in javascript?[
^].
—SA