If you are OK with HTML5 it can be accomplished without any JavaScript code at all...
<input type="number" name="textWeight" id="txtWeight" max="5" min="0" />
Otherwise, something like...
var input = document.getElementById('txtWeight');
input.addEventListener('change', function(e) {
var num = parseInt(this.value, 10),
min = 0,
max = 100;
if (isNaN(num)) {
this.value = "";
return;
}
this.value = Math.max(num, min);
this.value = Math.min(num, max);
});
This will only reset the values when the input looses focus, and clears out any input that can't be parsed as an integer...
OBLIGATORY WARNING
You should always perform adequate server-side validation on inputs, regardless of client-side validation.