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

**URL:** <https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076>\
**Category:** Rails\
**Created:** [January 17, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076 "2006-01-17T19:22:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 17, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076/1 "2006-01-17T19:22:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 17, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076/2 "2006-01-17T19:31:38Z")

</div>

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](http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm)

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 17, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076/3 "2006-01-17T19:34:39Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 17, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076/4 "2006-01-17T19:34:40Z")

</div>

On 1/17/06, James R. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 17, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076/5 "2006-01-17T19:34:41Z")

</div>

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](http://www.felgall.com/jstip44.htm)

- Jim

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 17, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/noob-numeric-only-input-can-it-be-done-on-client/62076/6 "2006-01-17T20:01:49Z")

</div>

Thanks very much for all the helpful advice everyone.

James

* * *

From: removed\_email\_address@domain.invalid  
[[mailto: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](http://www.felgall.com/jstip44.htm)

- Jim

On 1/17/06, Jim Campbell \< removed\_email\_address@domain.invalid [mailto: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](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
