我希望手机和PC都不允许字符只允许数字

时间:2018-11-24 07:19:52

标签: jquery html

我有用于输入和搜索按钮的代码以及仅允许号码的代码  进入桌面它不允许但字符但进入移动设备它允许我希望两者都不是字符

           跟踪     
<script>

    function isNumber(evt) {
        var iKeyCode = (evt.which) ? evt.which : evt.keyCode
        if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
            return false;

        return true;
    }    
</script>

1 个答案:

答案 0 :(得分:1)

请参阅此代码段,它将在两台PC n mobile上均可使用。

$( "input" ).on( "keypress", function(evt) {
        var iKeyCode = (evt.which) ? evt.which : evt.keyCode
        if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)){
           return false;
        }
        return true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />