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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James R. (Guest)
on 2006-01-17 20:22
(Received via mailing list)
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
Ben R. (Guest)
on 2006-01-17 20:31
(Received via mailing list)
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
Jim Campbell (Guest)
on 2006-01-17 20:34
(Received via mailing list)
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) {

}
James L. (Guest)
on 2006-01-17 20:34
(Received via mailing list)
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
Jim Campbell (Guest)
on 2006-01-17 20:34
(Received via mailing list)
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
James R. (Guest)
on 2006-01-17 21:01
(Received via mailing list)
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
This topic is locked and can not be replied to.