Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbers only please 1

Status
Not open for further replies.

fischadler

Programmer
May 31, 2002
258
MT
Please consider the code below:
Code:
 onkeypress="return '0123456789.'.indexOf(String.fromCharCode(event.keyCode))>-1"
I use it to stop people from entering text other than numeric values in a textbox. It works fine in IE but disallows all keystrokes in Firefox. Is there a way I can get it to work as it should in both browsers?

Thanks!

-Fischadler
 
This works for me in both IE and FF:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function numsOnly(evt) {
			return ('0123456789.'.indexOf(String.fromCharCode((evt) ? evt.which : event.keyCode)) > -1);
		}

		onload = function() {
			document.getElementById('myNumsOnlyField').onkeypress = numsOnly;
		}
	//-->
	</script>
</head>
<body>
	<form>
		<input type="text" id="myNumsOnlyField" />
	</form>
</body>
</html>

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
OK. I tried to test it on Firefox on my home PC that runs the IIS that comes with Windows XP Pro. However when I try to open an ASP site on my PC through FF I get this prompt:
prompt.jpg

I have no clue about what password it's talkin' about and running the same page through IE asks for no such thing.

-Fischadler
 
That's a very different problem, and one that should definately be asked in a different forum, I'd say.

If you save the code I gave as an plain old HTML file and run it locally, it works just fine.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You are absolutely right! For a moment I forgot that JavaScript does not require IIS. Your code sample works great!

Thanks!

-Fischadler
 
question:

Code:
function numsOnly(evt) {
            return ('0123456789.'.indexOf(String.fromCharCode((evt) ? evt.which : event.keyCode)) > -1);
        }

        onload = function() {
            document.getElementById('myNumsOnlyField').onkeypress = numsOnly;
        }

Q.) How about backspace, enter and tab? :D

Q.) So if I wanted it to accept text only I'd make another function called textOnly with 'abcde.........z' and vice versa?
... wait lemme try if it would work.... ok it did. But still no backspace, enter or tab.
 
You'd need to find the keycodes for backspace, enter and tab. From memory, they are 8, 13, and 9... although you can simply put an alert to return the keycode, I'm sure.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hey, if you're still looking for a number validation code, I wrote this faq216-6160

Let me know if you have any issues. This has been tested and works on IE, NS and FF.

____________________________________
Just Imagine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top