Javascript:如何在关键事件中使用最大值限制Textbox的值? [英] Javascript : How to restrict Textbox value with the max value on key events?

查看:61
本文介绍了Javascript:如何在关键事件中使用最大值限制Textbox的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我想使用按键事件将最大值限制为该值

我尝试使用下面的代码,并且效果很好

I have a text box i want to restrict that value for max value using key events

I try with the below code and its work fine

function AdjustKeyPress(e, object, value) {
        var KeyID = (window.event) ? event.keyCode : e.which;

        if ((KeyID >= 65 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <= 47) ||
                    (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126)) {
            return false;
        }

        if (KeyID == 32) {
            return false;
        }
        var recentChar = String.fromCharCode(e.which);
        if (object.value + recentChar > value) {
            return false;
        }
    }


当我们在文本框的末尾插入或按键时,它的工作正常.但是当我在两个值之间按下一个键时,那是错误的..


Its work fine when we insert or press key at the end of text box. but when i press a key in between value then its wrong..

<asp:TextBox ID="txtColorsize" MaxLength="30" onkeypress="javascript:return AdjustKeyPress(event,this,182)" runat="server">


如果我将最后的"2"值更改为正常,但是如果我将"8"替换为"9",则其变为"129"而不是"192"

请帮助我找到解决方法
预先感谢


if i change last ''2'' value its work fine,but If i replace ''8'' with ''9'' its becomes ''129'' instead of ''192''

Please help me to find the solution
Thanks in advance

推荐答案

不好的主意!稍后更好地验证输入,否则每次按键的验证都会给用户带来极大的困惑.

这就是为什么.假设您的最大值为900.用户输入799(有效),然后意识到有效值为789(仍然有效).用户可以删除"9"中的一个,然后输入"8".它将始终保持输入有效.或者,用户可以在已经输入"7"之后输入"8",以期在下一步中删除额外的"9".令用户惊讶的是,将显示一条错误消息.用户必须单击确定"(或类似的命令),然后再删除多余的"9",如果可能的话,可能会在行为中在您的住址中含糊了一些4个字母的单词.

你要吗? 使用输入时,最好稍后再验证输入.

同时,过滤掉输入中不需要的字符将非常有用.例如,您只能允许数字.不要忘记允许字符#8,退格-可能是由于历史原因,它被视为字符.
有关此类代码的示例,请参见
Bad idea! Better validate the input later, otherwise your validation per key press will create a great confusion in the user.

Here is why. Imagine your max value is 900. The user enters 799 (valid) and realized that the valid value would be 789 (still valid). The user can delete on of ''9'' and enter ''8''. It would keep input valid all the time. Alternatively, the user can enter ''8'' after already entered ''7'' in hope to delete extra ''9'' on next step. To the user surprise, an error message will be shown. The user will have to click "OK" (or something) and delete extra ''9'' after that, if it''s even possible, probably mumbling some 4-letter words at your address in the act.

Do you want it? Better validate the input later, at the moment it is used.

At the same time, filtering out of unwanted characters on input would be quite useful. You can allow only digits, for example. Don''t forget to allow the character #8, backspace — it is treated like a character, probably by historical reasons.
For example of such code, see how to restrict users to put the numbers in range of -4713 and +9999 in javascript?[^].

—SA


首先,使用"onkeyup"代替"onkeypress",这更准确!然后在这种情况下,计算文本框值的长度,如下所示:

Firstly, use the "onkeyup" instead of "onkeypress" which is more accurate! and then in that event calculate the length of the textbox value like so:

<input id="txtInput" value="" onkeyup="AdjustKeyPress(this.value);" />


function AdjustKeyPress( value )
{
  //change max value
  var maxValue = 200;
  if( value.length> maxValue )
  {
      //show error
  }
  else
  {
     //proceed
  }
}


这篇关于Javascript:如何在关键事件中使用最大值限制Textbox的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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