Hey, now i''m stuck. I''m using the following code in a keypress handler to determine of the pressed key was numeric. This is used for an edit in place, numeric box which is activated by clicking the number (which is a hyperlink) and deactivated by an enter press. if (e.keyCode==13) { //do some stuff to finish editing } if (!String.fromCharCode(e.keyCode).isNumeric()) { Event.stop(e); } Problem is, I can already see a few problems. 1) prevents copy & paste (which can be solved with a blur handler to strip non numeric characters out, but isn''t an issue in this particular usage scenario anyway) 2) the number keypad no longer works for entering numeric data; String.fromCharCode(e.keyCode) returns ''a'' for 1, ''b'' for 2, etc when I press keys on the numeric keypad but works fine for the numbers above the keys on my keyboard. What''s the workaround here? Gareth --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 10/18/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ... > Problem is, I can already see a few problems. > 1) prevents copy & paste (which can be solved with a blur handler to strip > non numeric characters out, but isn''t an issue in this particular usage > scenario anyway) > 2) the number keypad no longer works for entering numeric data; > String.fromCharCode(e.keyCode) returns ''a'' for 1, ''b'' for 2, etc when I > press keys on the numeric keypad but works fine for the numbers above the > keys on my keyboard.You''ll probably want to validate based on e.keyCode alone. Also beware that different browsers and different OSs will report different keyCodes for non-letter keys (see http://www.quirksmode.org/js/keys.html for a start) It gets complicated, so you may consider another approach such as an onblur validation or a check every few seconds. - Ken --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
What... a... mess. I am wondering if prototype should be expanded to cover over some of these browser differences? That aside, I guess i''m going to have to use [1,2,3].include(e.keyCode) where 1,2,3 are the valid keyCode values for the numbers on both the number strip and number pad. This opens up a bit of a can of worms with regards to modifier keys such as shift. Surely there''s a better way to determine if the pressed key was a number! Gareth On 10/19/07, Ken Snyder <kendsnyder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 10/18/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ... > > Problem is, I can already see a few problems. > > 1) prevents copy & paste (which can be solved with a blur handler to > strip > > non numeric characters out, but isn''t an issue in this particular usage > > scenario anyway) > > 2) the number keypad no longer works for entering numeric data; > > String.fromCharCode(e.keyCode) returns ''a'' for 1, ''b'' for 2, etc when I > > press keys on the numeric keypad but works fine for the numbers above > the > > keys on my keyboard. > > You''ll probably want to validate based on e.keyCode alone. Also > beware that different browsers and different OSs will report different > keyCodes for non-letter keys (see > http://www.quirksmode.org/js/keys.html for a start) > > It gets complicated, so you may consider another approach such as an > onblur validation or a check every few seconds. > > - Ken > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---