Hey all, Just had a need for this, wondering if it is a candidate for inclusion as it is only a single line and I can''t seem to spot any other way of acheiving the same functionality in less code. String.prototype.isNumeric = function() { return (this.match(/^\d+$/) !null); } I used it in the context of: Event.observe(el,''keypress'',function (e) { if (!this.value.isNumeric()) Event.stop(); }); Admittedly, it doesn''t cater for decimal points and other locales, but it''s a start? The regex came from inside the selector code in prototype, so it''s used once at present formula.match(/^\d+$/) 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:> ... > String.prototype.isNumeric = function() { return (this.match(/^\d+$/) !> null); } > > I used it in the context of: > Event.observe(el,''keypress'',function (e) { if (!this.value.isNumeric()) > Event.stop(); }); > Admittedly, it doesn''t cater for decimal points and other locales, but it''s > a start? > ...don''t forget scientific notation (e.g. 7e5) :P here''s a simple approach: String.prototype.isNumeric = function() { return parseFloat(this) + '''' == parseFloat(this); }; ''foo''.isNumeric(); // false ''7e5''.isNumeric(); // true ''7e-5''.isNumeric(); // true ''0.2''.isNumeric(); // true ''-31''.isNumeric(); // true ''+02''.isNumeric(); // true - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That''s definitely more reliable for validating a string, thanks Ken. 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: > > ... > > String.prototype.isNumeric = function() { return (this.match(/^\d+$/) !> > null); } > > > > I used it in the context of: > > Event.observe(el,''keypress'',function (e) { if (!this.value.isNumeric()) > > Event.stop(); }); > > Admittedly, it doesn''t cater for decimal points and other locales, but > it''s > > a start? > > ... > don''t forget scientific notation (e.g. 7e5) :P > > here''s a simple approach: > > String.prototype.isNumeric = function() { > return parseFloat(this) + '''' == parseFloat(this); > }; > ''foo''.isNumeric(); // false > ''7e5''.isNumeric(); // true > ''7e-5''.isNumeric(); // true > ''0.2''.isNumeric(); // true > ''-31''.isNumeric(); // true > ''+02''.isNumeric(); // true > > > - Ken Snyder > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---