On 10/18/07, Gareth Evans <[EMAIL PROTECTED]> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---