Nobody likes to be called a dummy by a dummy.

another stupid question you can blame on my fever

How in the hell do I prevent any non-digit characters from being entered into an HTML text input using javascript?

I found two tutorials but the code in them did not work in Firefox.
Permalink Mark Warner 
January 18th, 2006
function onlyNumbers(inputString)
{
var searchForNumbers = /\D+\_+\W+\s+\S+/
(searchForNumbers.test(inputString)) ? return false : return true;
}
Permalink Dreamweaver 
January 18th, 2006
Nope. I attached your function to the onkeypress event, setting inputString toevent.keyCode, and no dice, I can still enter alpha and special characters in the field.

wtf, this seems simple.
Permalink Mark Warner 
January 18th, 2006
oh wait duh.
Permalink Mark Warner 
January 18th, 2006
Didn't this work?

http://www.mredkj.com/tutorials/validate.html

It works on his page for FireFox.
Permalink sharkfish 
January 18th, 2006
I don't know why I'm trying to code while sick.
Permalink Mark Warner 
January 18th, 2006
It works on his page but for some reason will not work in my app. I think it has to do with the XUL element I'm trying to use and not the javascript itself.
Permalink Mark Warner 
January 18th, 2006
" I don't know why I'm trying to code while sick."
Ummm... wouldn't that about cover the last two years of your life, dude? Just asking.
Permalink Star Wars Kid 
January 19th, 2006
I meant sick with a fever and shakes and vomiting :P

Crohn's is no party but it's a latent kind of sickness most of the time.

Smartass.
Permalink Generic Error 
January 20th, 2006

This topic was orginally posted to the off-topic forum of the
Joel on Software discussion board.

Other topics: January, 2006 Other topics: January, 2006 Recent topics Recent topics