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

发布于 10-22 05:54 字数 688 浏览 7 评论 0原文

我放置了一个文本框....我想对其进行限制..

不允许在文本框中输入数字和特殊字符...

我该如何在Javascript中使用onkeypress事件???

我的代码是...

<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>

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 ???

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多2024-10-29 05:54:42

文本框中不允许输入数字和特殊字符...

不要通过 keypress 过滤来执行此操作。它对用户不利、令人困惑、搞乱其他控制按键,并且很容易通过使用其他输入方法(例如复制/粘贴、拖/放、表单填充器、拼写检查器、IME)来击败它......

相反,有一个明显的警告当用户放入意外内容时指示用户,例如:

<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>

如果需要,您可以使用 addEventListener 作为事件附件,但请注意,这在 IE<9 中不起作用,对于 IE<9您将需要 attachEvent 模型。 (event.returnValue 特定于 IE attachEvent 模型,不能与 addEventListener 一起使用,后者使用 event.preventDefault()< /code> 相反。)

that digits and special characters should not be allowed to input in textbox...

Don't do this through keypress filtering. 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...

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. (event.returnValue is specific to the IE attachEvent model and will not work with addEventListener, which uses event.preventDefault() instead.)

情深缘浅2024-10-29 05:54:42

请尝试以下操作:

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

Try the following:

document.getElementById('TX1').addEventListener('keypress', function(event) {
        if (event.keyCode == 13) {
        handleKeyPress();
    }
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文