js实现键盘数字输入

<html>
<head>
<meta charset="UTF-8">
</head>
<script>
function return_value(e){
    var e = e || window.event;
    var charCode=e.keyCode || e.charCode;
    if(charCode<48||charCode>75)
    {
        alert("对不起,你输入的不是数字字符!");
        e.returnValue=false;
    }
}
</script>
<body>
    <textarea name="textarea" onKeyPress="return return_value(event)"></textarea>
    <!-- onKeyPress="return return_value(event) 支持firefox、IE、chrome
        onKeyPress="return_value() 只支持IE、chrome
    -->
</body>
</html>


只能输入字母的

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<script type="text/javascript">
    function noNumbers(e)
    {
        var keynum;
        var keychar;
        var numcheck;
        if(window.event) // IE
        {
            keynum = e.keyCode;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which;
        }
        keychar = String.fromCharCode(keynum);
        numcheck = /\d/;
        return !numcheck.test(keychar);
    }
</script>
<body>
<form>
    <input type="text" onkeypress="return noNumbers(event)" />
</form>
</body>
</html>


posted on 2016-07-16 21:11  菜鸟Z  阅读(608)  评论(0编辑  收藏  举报

导航