阻止用户输入最大值的输入 [英] Prevent user from typing in input at max value

查看:93
本文介绍了阻止用户输入最大值的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果价值超过100,我希望阻止用户输入更多内容。到目前为止,我在阅读不同帖子时有以下内容:

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

要确认我希望值不是字符串长度,并且不能超过100。 / p>

然而,这并不妨碍该领域的进一步投入。我错过了什么。



解决方案: http:// jsfiddle。 net / 9TP3e /

解决方案

检查keyup为时已晚,添加角色的事件已经发生。你需要使用keydown。但是你也想确保值不是> 100,所以你也需要使用keyup来允许js检查值。



你也有允许人们删除该值,否则,一旦超过100,就无法更改任何内容。

  $('。equipCatValidation') .on('keydown keyup',function(e){
if($(this).val()> 100
&& e.keyCode!== 46 //删除键码
&& e.keyCode!== 8 //退格键码
){
e.preventDefault();
$(this).val(100);
}
});

http://jsfiddle.net/EBs33/1/


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.

Solution: http://jsfiddle.net/9TP3e/

解决方案

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.

$('.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/EBs33/1/

这篇关于阻止用户输入最大值的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆