Mr. Shopinson was thinking of developing a web application but needs to add some kind of validation where the user can only be able to type in Number only input box with range restriction.
Actually this and others alike validation have been the order of the day on search by other web developer as I am.
WHAT i did.
I tried to solve this problem using html 5 but up to no avail. you may also like to read about php ALLOW only a range of numbers from a FORM field
HTML 5 code that MIGHT allow only numbers within a range in text field
Definitely I tried HTML5 constraint validation API but it was supported mainly by some web browsers.
<input type="number" min="2" max="7" step="1" id="n" oninput="(validity.valid)||(value='');">
Then I used the snippet of basic code below either To validate and auto-correct the input depending on validity states
<input type="number" min="1" max="10" id="numberSize" oninput="(!validity.rangeOverflow||(value=10)) && (!validity.rangeUnderflow||(value=1)) &&(!validity.stepMismatch||(value=parseInt(this.value)));">
This code above will send
- all inputs >max to max
- inputs < min to min
and truncate decimal values.
jQuery plugin to allow only numbers in certain range in a text field?
Plugins/Validation/Methods/range should get you started with making sure only a number in a certain range is entered.
onkeypress="return isNumberKey(event)" onkeyup="this.value = fnc(this.value, 0, 10)" into my basic html codes as shown below
<input type="text" name="textWeight" id="txtWeight" maxlength="2" onkeypress="return isNumberKey(event)" onkeyup="this.value = fnc(this.value, 0, 10)"/>
Maxlength="2" is a html validation method that makes sure that only two character of number or text is the text field.