简体   繁体   English

按键事件的addEventListener中的问题……仅用于限制输入字符

[英]problem in addEventListener for keypress event… for restricting the input charcters only

I have placed one textbox.... I want to put restriction on it .. 我已经放置了一个文本框..我想对其进行限制..

that digits and special characters should not be allowed to input in textbox... 不允许在文本框中输入数字和特殊字符...

how can i do using onkeypress event in Javascript ??? 我该如何在Javascript中使用onkeypress事件?

my code is ... 我的代码是...

<script>

 function event()
 {
    document.getElementById("TX1").addEventListener("keypress", handleKeyPress, false);


 }


function handleKeyPress(e)
{
    var restricted = "0123456789_#!";
    var key = e.keyCode || e.which;
    var i=0;
    for(;i<restricted.length;i++)
    {
      if (restricted.charCodeAt(i) == key) 
      {
        e.returnValue = false;

      }

         return true;
}

</script>

<body onLoad="event();">

<input type="text" name="T1" id="TX1" size="27" maxlength="35" >

</body>

that digits and special characters should not be allowed to input in textbox... 不允许在文本框中输入数字和特殊字符...

Don't do this through keypress filtering. 不要通过keypress过滤来做到这一点。 It's user-hostile, confusing, messes up other control keypresses and is easy to defeat by using other methods of input, such as copy/paste, drag/drop, form fillers, spellcheckers, IMEs... 它对用户不利,令人困惑,使其他控制键混乱,并且易于通过使用其他输入方法来克服,例如复制/粘贴,拖放/填充,表格填充,拼写检查,IME等。

Instead, have a warning that visibly directs the user when they've put something unexpected in, eg.: 相反,当用户放置了意外内容时,应发出警告以明显指导用户,例如:

<input type="text" name="T1" id="TX1"/>
<div id="TX1_help">
    Please do not use numbers or ‘!’, ‘_’ or ‘#’ characters in this field.
</div>

<script type="text/javascript">
    var tx1= document.getElementById('TX1');
    var tx1help= document.getElementById('TX1_help');

    function checkTX1() {
        var isok= /^[^0-9_#!]*$/.test(tx1.value);
        tx1help.style.display= isok? 'none' : 'block';
    }
    checkTX1();

    // Re-check the field every time a key is pressed or other change event.
    // To ensure it is still checked in a timely fashion in the case of
    // non-keyboard events, add a timer as well.
    //
    tx1.onchange=tx1.onkeyup= checkTX1;
    setInterval(checkTX1, 500);
</script>

You can use addEventListener for the event attachment if you want to, but be aware this won't work in IE<9, for which you would need the attachEvent model. 如果需要,可以将addEventListener用作事件附件,但是请注意,这在IE <9中将不起作用,为此,您需要attachEvent模型。 ( event.returnValue is specific to the IE attachEvent model and will not work with addEventListener , which uses event.preventDefault() instead.) event.returnValue特定于IE attachEvent模型,不适用于addEventListener ,后者使用event.preventDefault()代替。)

Try the following: 请尝试以下操作:

document.getElementById('TX1').addEventListener('keypress', function(event) {
        if (event.keyCode == 13) {
        handleKeyPress();
    }
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM