Handle key using javascript onkeydown

若如初见. 提交于 2019-12-02 04:09:22

Here in your code you are testing the regular expression defined with the keycode, so every chearactes on the keyboard will be allowed since the keycode of every key is numbers, so you will not get the result what you expect. Instead of using the regular expression try the below code

<html>
<head>
<script type="text/javascript">
function verifyKey(e)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;


    if((keycode>=48 && keycode<=57))
    {alert("if")
        return true;
    }
    else if((keycode == 188)||(keycode == 190))
    {alert("elseif");
        return true;
    }
    else
    {alert("else")
        return false;
    }
}


</script>
</head>
<body>
<input type="text" onkeypress="return verifyKey(event)" />
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!