RegEx to avoid numbers?

Yes I’ve tried it myself and even went to
Regular Expression Library with no luck. I suppose my request is
too basic. All I want to do is prevent users from adding numbers to a
field. Any character (UTF8) is fine, except [0-9]. What RegEx returns
true for any number of characters as long as there are no numbers?

def no_digits(field)
(field =~ /\d/) == nil
end

On 12/15/06, Taylor S. [email protected] wrote:

Yes I’ve tried it myself and even went to
Regular Expression Library with no luck. I suppose my request is
too basic. All I want to do is prevent users from adding numbers to a
field. Any character (UTF8) is fine, except [0-9]. What RegEx returns
true for any number of characters as long as there are no numbers?

If it has to be a regexp to match such a string:

/\A[^\d]*\z/

But if possible, it’s usually easier to invert the operator:

str !~ /\d/

For example, in a validation:

def validate
thing !~ /\d/ or
errors.add :thing, “cannot contain numbers”
end

HTH.

Thanks everyone - I don’t know why I couldn’t find that anywhere!

Hi,

Yes I’ve tried it myself and even went to
Regular Expression Library with no luck. I suppose my
request is
too basic. All I want to do is prevent users from adding numbers to a
field. Any character (UTF8) is fine, except [0-9]. What RegEx
returns
true for any number of characters as long as there are no numbers?

/^[^0-9]*$/ should work for you.

Regards,
Carl D. | 29degrees

29degrees.co.uk - Bespoke Web Application Development.
codegolf.com - Smaller IS better.

On 12/15/06, [email protected] [email protected] wrote:

Hi –

On Fri, 15 Dec 2006, George O. wrote:

/\A[^\d]*\z/

You can also use \D (non-digit) instead of [^\d]. (I agree that !/\d/
is better anyway though.)

D’oh! Thanks for the reminder, David. :slight_smile:

Hi –

On Fri, 15 Dec 2006, George O. wrote:

/\A[^\d]*\z/

You can also use \D (non-digit) instead of [^\d]. (I agree that !/\d/
is better anyway though.)

David


Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)