views:

423

answers:

4

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.

+1  A: 

You just need to test the value of the field after each keystroke and before it is sent to the field (for example with a keypress event handler) - if the new keystroke would create a value too high then reject the key press.

For example - if the current value is 20 and the user tries to type a third number, you should reject the key press by returning false from the event handler. Basically the only third character that you should allow is "0" and only if the current field value is "10".

Guss
I would advise not using this method. At least change it to the blur event. People don't like their input getting validated before they're done entering. Not to mention, it's possible to populate text boxes without keypresses. (mouse paste)
recursive
+2  A: 

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.

Crimson
Thanks a lot. How would I bind the event so that it's not inline?
Jourkey
-1 Coercing acceptable values is a horrible interface design. An inattentive user could easily enter unintended data this way. You should flag the data as invalid and let the user choose how to adjust it. Perhaps, they've just typed into the wrong box and the value really belongs elsewhere, where it would be valid.
tvanfosson
I definitely see your point. But isn't this exactly what a slider does? When you try to go over 100, it just forces you back to 100/the max.
Jourkey
@Jourkey -- yes, but it's a different type of interface element. You can't force it past the limit. With a textbox you could type 134 and not notice that it silently changes it to 100. Ideally you'd use an input with validation (server side) that gets hidden by javascript and replaced by a slider that fills in the input values. That way it presents the appropriate interface for most users, but degrades gracefully when there is no javascript.
tvanfosson
A: 
<input type="text" id="test_id" />
<script>
document.getElementById("test_id").onkeyup=function(){
    var input=parseInt(this.value);
    if(input<0 || input>100)
    alert("Value should be between 0 - 100");
    return;
}    
</script>
Xinus
I'd be ok with this if you didn't use alerts. I hate popups for this type of notification.
tvanfosson
+3  A: 

I'd suggest using the jQuery Validation plugin. Very flexible and extensible. To handle your range query the following. It will valied that any input in your form marked with the "percentage" class is present and falls in the given range.

$("#myform").validate({
  rules: {
    percentage: {
      required: true,
      range: [0, 100]
    }
  }
});

<input id="percent" name="percent" class="percentage" />

The reason that I suggest using the plugin is that it handles many validation situations out of the box, relieving you of the responsibility to write validation code for many scenarios -- you can expend your creative effort in better ways. It's also used by many people and so has the added advantage of being improved by a large community.

Update: Also, you may want to consider an alternative input mechanism, such as a slider (demo) that will constrain the data in a way that isn't subject to user error -- or at least not this particular type of error.

tvanfosson
Thanks for your help. I have a slider as well as an input box. The slide is hooked to the input box and vice versa. The reason why I'm not using validation is because entering a too big value would just set the slider to 100%, but the input box would not follow, and that's just odd. Also the blur event might take the user away from the page / hide the input field, and the user would be left with a broken value without ever seeing the error.
Jourkey
@Jourkey -- see my comment on the accepted answer. I would hide the input when creating the slider. If that's not an option, at the very least I would throw up a warning and not let the form submit rather than coercing the value.
tvanfosson