Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Only Allow Numbers In A Text Field

DZone's Guide to

Only Allow Numbers In A Text Field

·
Free Resource
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:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}