JS控制只能输入数字

<script type="text/javascript">
function inputNumericMinasTextBox() {
    //Ctrl、Altが押されている時は何もしない
    if (event.ctrlKey) return;
    if (event.altlKey) return;

    //キーコードを取得する
    var pushKey;
    pushKey = event.keyCode;

    //数字の時は何もしない(下段はテンキー)
    if ((pushKey >= 0x30) && (pushKey <= 0x39) && !event.shiftKey) return;
    if ((pushKey >= 0x60) && (pushKey <= 0x69)) return;

    //以下の特殊キーは何もしない
    switch (pushKey) {
        case 0x08: return; //BackSpace
        case 0x09: return; //Tab
        case 0x0D: return; //Enter
        case 0x23: return; //End
        case 0x24: return; //Home
        case 0x25: return; //←
        case 0x27: return; //→
        case 0x2D: return; //Insert
        case 0x2E: return; //Delete
        case 0x10: return; //Shift
        case 0x11: return; //Ctrl
        case 0x12: return; //Alt
        case 0x6D: return; //-(TenKey)
        case 0xBD:         //-(FullKey)
            if (event.shiftKey) {   //Shiftキーが押されている場合は無効とする
                event.returnValue = false;
                return;
            } else {
                return; //-
            }
    }
    //上記以外はキー入力が無かったことにする
    event.returnValue = false;
}
</script>

 

<input type="text" style="width:200px" onkeydown="inputNumericMinasTextBox()"/>

转载于:https://www.cnblogs.com/flying607/p/3314398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值