Over a million developers have joined DZone.

Only Allow Numbers In A Text Field

·
Place this between the opening and closing body tag.  It has been successfully tested in Safari 4, Firefox 3, IE6, and IE7


function onlyNumbers(evt) {
	var e = evt
	if(window.event){ // IE
		var charCode = e.keyCode;
	} else if (e.which) { // Safari 4, Firefox 3.0.4
		var charCode = e.which
	}
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
	return true;
}


onkeypress = "onlyNumbers(event)"
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}