JavaScript による入力制限( 0〜9 と BackSpace しか入力できないフィールド ) :【WEB】

0〜9 と BackSpace しか入力できないフィールド

以下のコードでは、IE の場合のみカーソル移動キーが入力可能です。
実際はそのほうが簡単で都合が良いのですが、厳密に他のブラウザと
同じように 0〜9 と BackSpace のみにしたい場合は、onkeydown に記述する必要があります
<script type="text/javascript">

function numField(evt) {

	var btype = window.navigator.userAgent.toLowerCase();

	// *************************************************
	// Firefox
	// *************************************************
	if ( btype.indexOf("firefox") > -1 ) {
		if ( 48 <= evt.charCode && evt.charCode <= 57 ) {
			return;
		}
		if ( evt.charCode == 0 ) {
			if ( evt.keyCode == 8 ) {
				return;
			}
		}
		evt.preventDefault();
	}

	// *************************************************
	// IE、Opera
	// *************************************************
	if ( btype.indexOf("msie") > -1 || btype.indexOf("opera") > -1	) {
		if ( 48 <= evt.keyCode && evt.keyCode <= 57 ) {
			return;
		}
		if ( evt.keyCode == 8 ) {
			return;
		}
		evt.returnValue = false;
	}

}

</script>

<INPUT type=text onkeypress="numField(event)">
IE の場合のみ、以下のイベントを onkeydown に追加します。
( その場合は、onkeypress の IE の記述は必要ありません )
function ieNumField(evt) {

	var btype = window.navigator.userAgent.toLowerCase();
	// *************************************************
	// IE
	// *************************************************
	if ( btype.indexOf("msie") > -1 ) {
		if ( 48 <= evt.keyCode && evt.keyCode <= 57 ) {
			return;
		}
		if ( 96 <= evt.keyCode && evt.keyCode <= 105 ) {
			return;
		}
		if ( evt.keyCode == 8 ) {
			return;
		}
		evt.returnValue = false;
	}

}
この手の処理は、ブラウザ毎に対応が必要となる為、
一般 WEB では避けたほうが良いでしょう。

業務アプリ + IE + 運用規則 で最も使いやすい環境になります