NOOB: Numeric only input...can it be done on client?

I was wondering if there’s a way to limit what a user is typing on the
keyboard to only valid numeric input. Is there a simple plugin or
something
which would allow this? I guess with AJAX it could also be one. Has
anyone
already solved this requirement? Thanks in advance.

James

Javascript should get the job done. A quick google for “numeric entry
only
javascript” had this result… Might work well:

http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm

Sure, and there’s no need to complicate it with a server call. A simple
way
to do it is to define a string of characters (in your case, numbers),
then
have each keystroke check that string for characters. Something like
this
would work:

var numerals = “0123456789”;
function checkInput(myField) {

}

On 1/17/06, James R. [email protected] wrote:

I was wondering if there’s a way to limit what a user is typing on the
keyboard to only valid numeric input. Is there a simple plugin or something
which would allow this? I guess with AJAX it could also be one. Has anyone
already solved this requirement? Thanks in advance.

Keep in mind that even if you get this to work in the browser, your
server-side code should never trust this data to be in a valid format.
You still need to do validation on the server.

As for your question, it seems like this would be pretty easy with
JavaScript and the onkeypress event handler.

– James

Hah - oops, I hit “Enter” by accident.

I was going to type in a function, but Googling for “Javascript restrict
input” turns up a good example as is (although i’m not sure if it’s the
most
graceful):

http://www.felgall.com/jstip44.htm

  • Jim

Thanks very much for all the helpful advice everyone.

James


From: [email protected]
[mailto:[email protected]] On Behalf Of Jim Campbell
Sent: Tuesday, January 17, 2006 11:57 AM
To: [email protected]
Subject: Re: [Rails] NOOB: Numeric only input…can it be done on
client?

Hah - oops, I hit “Enter” by accident.

I was going to type in a function, but Googling for “Javascript restrict
input” turns up a good example as is (although i’m not sure if it’s the
most
graceful):

http://www.felgall.com/jstip44.htm

  • Jim

On 1/17/06, Jim Campbell < [email protected] mailto:[email protected]

wrote:

Sure, and there’s no need to complicate it with a server call. A simple
way
to do it is to define a string of characters (in your case, numbers),
then
have each keystroke check that string for characters. Something like
this
would work:

var numerals = “0123456789”;
function checkInput(myField) {

}

On 1/17/06, James R. < mailto:[email protected]
[email protected]> wrote:

I was wondering if there’s a way to limit what a user is typing on the
keyboard to only valid numeric input. Is there a simple plugin or
something
which would allow this? I guess with AJAX it could also be one. Has
anyone
already solved this requirement? Thanks in advance.

James