# RegEx to avoid numbers?

**URL:** <https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447>\
**Category:** Rails\
**Created:** [December 15, 2006, 7:58am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447 "2006-12-15T07:58:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 15, 2006, 7:58am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/1 "2006-12-15T07:58:51Z")

</div>

Yes I’ve tried it myself and even went to  
[Regular Expression Library](http://regexlib.com/Default.aspx) 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?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 15, 2006, 9:12am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/2 "2006-12-15T09:12:15Z")

</div>

def no\_digits(field)  
(field =~ /\d/) == nil  
end

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 15, 2006, 9:48am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/3 "2006-12-15T09:48:19Z")

</div>

On 12/15/06, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes I’ve tried it myself and even went to  
> [Regular Expression Library](http://regexlib.com/Default.aspx) 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.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 15, 2006, 10:20am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/4 "2006-12-15T10:20:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 15, 2006, 9:56am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/5 "2006-12-15T09:56:19Z")

</div>

Hi,

> Yes I’ve tried it myself and even went to  
> [Regular Expression Library](http://regexlib.com/Default.aspx) 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](http://29degrees.co.uk) - Bespoke Web Application Development.  
[codegolf.com](http://codegolf.com) - Smaller _IS_ better.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 17, 2006, 11:10am UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/6 "2006-12-17T11:10:02Z")

</div>

On 12/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 15, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/regex-to-avoid-numbers/79447/7 "2006-12-15T13:14:34Z")

</div>

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](http://www.manning.com/black))  
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](http://www.rubypal.com))
