I am creating a form and i want the input tag to accept numbers only and i also want to limit the numbers to be from 0 - 20.
This my html code:
<input type="text" name="three" class="actual" id="three" maxlength="2" onkeypress="return ForNumbers(event)" />
This is my javascript code:
<script type="text/javascript">
function ForNumbers(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
Use html type number and inline elements
min="1"
andmax="20"
to limit your input field.If you need to support old browsers, try this:
http://jsfiddle.net/3StSM/2/
It also works fine with:
Should be able to do something like this:
Simple as that if that's what you mean.
You could get this to run when the value for the input changes (
onChange
) that way they can see the limit.Full function:
HTML:
Javascript:
Working Demo