Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  150] [ 4]  / answers: 1 / hits: 103371  / 15 Years ago, sat, september 5, 2009, 12:00:00

I have an input box that takes values from 0-100. I want to prevent users from writing anything larger or smaller.



I've been using the jquery keyfilter plugin to limit the input of a field:
http://code.google.com/p/jquery-keyfilter/



This plugin can limit the input to numerals, but not within a range. How do I prevent users from entering numbers that would exceed the range. Thanks.


More From » jquery

 Answers
17

Use plain Javascript like this:



<script>
function handleChange(input) {
if (input.value < 0) input.value = 0;
if (input.value > 100) input.value = 100;
}
</script>


Then declare the input box like this:



<input type=text onchange=handleChange(this); />


Because you have hooked this function to onchange(), it will work even if a user copy / pastes something into the input box.


[#98751] Wednesday, September 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sonja

Total Points: 541
Total Questions: 113
Total Answers: 114

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
sonja questions
Mon, Nov 30, 20, 00:00, 4 Years ago
Sun, Oct 11, 20, 00:00, 4 Years ago
Thu, May 21, 20, 00:00, 4 Years ago
Sun, Nov 10, 19, 00:00, 5 Years ago
Mon, Aug 26, 19, 00:00, 5 Years ago
;