Prevent user from typing in input at max value

2020-02-25 23:31发布

I'd like the user to be blocked from typing more if the value is over 100. So far I have the following from reading different posts:

$('.equipCatValidation').keyup(function(e){
        if ($(this).val() > 100) {               
           e.preventDefault();                
        }
    });

To confirm I want the value not the string length, and it can't be above 100.

However this is not preventing further input in the field. What am I missing.

8条回答
姐就是有狂的资本
2楼-- · 2020-02-25 23:57

Basically keypress events are fired before accepting the current value. So when you press on any key, keypress event is subscribed but you don't get the updated value/result for the recently pressed key. So, to get the last pressed key we can use the fromCharCode method and concat it with the value we got from the textbox. That's it,

HTML :

<input id="inputBox" type="text" />

jQuery :

$("#inputBox").on("keypress", function(e){
    var currentValue = String.fromCharCode(e.which);
    var finalValue = $(this).val() + currentValue;
    if(finalValue > 100){
        e.preventDefault();
    }
});

jsFiddle

查看更多
我只想做你的唯一
3楼-- · 2020-02-26 00:06

It is bad UI to disable the input if a user inputs a bad value. I'm assuming you simply want to put a max value that the user cannot go over. If so, you can either clamp the value, or use the max attribute in your markup:

  <form>
    <input type='number' max='100'>
  </form>

If you input an invalid value, the input will turn red, and you cannot submit the form.

查看更多
▲ chillily
4楼-- · 2020-02-26 00:06
$('.equipCatValidation').on('keypress', function(e){
    var $element = $(this);
    var value = Number($element.val());
    var key = Number(e.key);

    if (Number.isInteger(key)) {
        value = Number("" + value + key);
    }

    if (value > 100) {
        return false;
    }
});
查看更多
手持菜刀,她持情操
5楼-- · 2020-02-26 00:09

In response to Popnoodles answer above,

It is considered a good practice to use the type-safe equality operators === and !== instead of their regular counterparts == and != as in Javascript it can/may cause unexpected type coercions. So it should be

    $('.equipCatValidation').on('keydown keyup', function(e){
    if ($(this).val() > 100 
        && e.keyCode !== 46 // keycode for delete
        && e.keyCode !== 8 // keycode for backspace
       ) {
       e.preventDefault();
       $(this).val(100);
    }
});
查看更多
女痞
6楼-- · 2020-02-26 00:13

Checking keyup is too late, the event of adding the character has already happened. You need to use keydown. But you also want to make sure the value isn't > 100 so you do need to also use keyup to allow js to check the value then too.

You also have to allow people to delete the value, otherwise, once it's > 100 nothing can be changed.

<input class="equipCatValidation" type="number" />

 

$('.equipCatValidation').on('keydown keyup', function(e){
    if ($(this).val() > 100 
        && e.keyCode !== 46 // keycode for delete
        && e.keyCode !== 8 // keycode for backspace
       ) {
       e.preventDefault();
       $(this).val(100);
    }
});

http://jsfiddle.net/wvye2u0t/

查看更多
别忘想泡老子
7楼-- · 2020-02-26 00:14

Here's a solution for those using modern vanilla Javascript:

Just snap the value back down to the max when the user focuses away from the input.

You would set the input to a number type and the max value

<input type="number" max="100">

and then add a function to the onblur method of the element

document.querySelector('input[max]').onblur = function (event) {
    // If the value is less than the max then stop
    if (Number(event.target.value) < event.target.max) return
    // Snap the value to the max
    event.target.value = event.target.max
}

You can also use oninput instead of onblur but that may cause the user to have to fight the input in certain situations.

Example

查看更多
登录 后发表回答