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()"/>

posted @ 2013-09-11 13:42  剑握在手  阅读(337)  评论(0)    收藏  举报
返回顶部↑