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


#1

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


#2

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


#3

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) {

}


#4

On 1/17/06, James R. removed_email_address@domain.invalid 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


#5

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

#6

Thanks very much for all the helpful advice everyone.

James


From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Jim Campbell
Sent: Tuesday, January 17, 2006 11:57 AM
To: removed_email_address@domain.invalid
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 < removed_email_address@domain.invalid mailto:removed_email_address@domain.invalid

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:removed_email_address@domain.invalid
removed_email_address@domain.invalid> 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