# Text validation

**URL:** <https://www.ruby-forum.com/t/text-validation/131409>\
**Category:** Rails\
**Created:** [March 9, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/text-validation/131409 "2008-03-09T16:46:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![megax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@megax](https://www.ruby-forum.com/u/megax)\
**Post date:** [March 9, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/text-validation/131409/1 "2008-03-09T16:46:12Z")

</div>

Hi

I want to add some validation to my ruby site, and I want to stop my  
users putting in certain words for example dugs or stolen. I know ruby  
has validation commands and I was wondering if there was one for textbox  
input or would I have to make my own?

---

<div class="post-metadata">

**Author:** ![megax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@megax](https://www.ruby-forum.com/u/megax)\
**Post date:** [March 9, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/text-validation/131409/2 "2008-03-09T17:03:07Z")

</div>

Alan Red wrote:

> I want to add some validation to my ruby site, and I want to stop my  
> users putting in certain words for example dugs or stolen. I know ruby  
> has validation commands and I was wondering if there was one for textbox  
> input or would I have to make my own?

You could add something like an observer to your form input constantly  
checking for bad words, but that would be costly for large input fields  
and wouldn’t catch someone feeding data to the action receiving the form  
data directly.

These things are best put into the model. Say you had a model Advert  
with attribute “body” which you wanted to protect, then use:

validates\_exclusion\_of :body, :in =\> %w(dugs stolen), :message =\> “bad  
word used”

For more general checking (for example to include case insensitive  
checking) override the #validate method:

[http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M001316](http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M001316)

---

<div class="post-metadata">

**Author:** ![megax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@megax](https://www.ruby-forum.com/u/megax)\
**Post date:** [March 9, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/text-validation/131409/3 "2008-03-09T18:00:05Z")

</div>

Mark B. wrote:

> Alan Red wrote:
> 
> > I want to add some validation to my ruby site, and I want to stop my  
> > users putting in certain words for example dugs or stolen. I know ruby  
> > has validation commands and I was wondering if there was one for textbox  
> > input or would I have to make my own?
> 
> You could add something like an observer to your form input constantly  
> checking for bad words, but that would be costly for large input fields  
> and wouldn’t catch someone feeding data to the action receiving the form  
> data directly.
> 
> These things are best put into the model. Say you had a model Advert  
> with attribute “body” which you wanted to protect, then use:
> 
> validates\_exclusion\_of :body, :in =\> %w(dugs stolen), :message =\> “bad  
> word used”
> 
> For more general checking (for example to include case insensitive  
> checking) override the #validate method:
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M001316)

Works perfectly, thanks for the help - surprising how straight forward  
that is to code really. Ruby on rails real is much easier than I  
believed.
