# How to create a custom validator

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-custom-validator/121160>\
**Category:** Rails\
**Created:** [November 24, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/how-to-create-a-custom-validator/121160 "2007-11-24T18:12:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 24, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/how-to-create-a-custom-validator/121160/1 "2007-11-24T18:12:42Z")

</div>

i was wondering how can i create my own custom validates\_ function. e.g.  
i am validating email adresses on a lot of places and would like to  
achieve the following

class User  
validates\_email :email  
end

does some has any hints?

with the same technique i would also like to do the following

class User  
strip\_attributes :firstname, :lastname  
end

this would automatically ensure that the attributes firstname and  
lastname are stored always without preceeding and trailing spaces.  
thanks for your help…

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 24, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/how-to-create-a-custom-validator/121160/2 "2007-11-24T18:42:06Z")

</div>

On Nov 24, 9:12 am, Michal G. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> with the same technique i would also like to do the following
> 
> class User  
> strip\_attributes :firstname, :lastname  
> end
> 
> ## this would automatically ensure that the attributes firstname and lastname are stored always without preceeding and trailing spaces. thanks for your help…
> 
> Posted viahttp://www.ruby-forum.com/.

these are good

[http://www.railsjitsu.com/ruby-on-rails/writing-your-own-custom-validators/](http://www.railsjitsu.com/ruby-on-rails/writing-your-own-custom-validators/)  
[http://paulsturgess.co.uk/articles/show/33-how-to-write-custom-validation-in-ruby-on-rails](http://paulsturgess.co.uk/articles/show/33-how-to-write-custom-validation-in-ruby-on-rails)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 24, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-create-a-custom-validator/121160/3 "2007-11-24T18:45:02Z")

</div>

On Nov 24, 9:12 am, Michal G. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> with the same technique i would also like to do the following
> 
> class User  
> strip\_attributes :firstname, :lastname  
> end
> 
> ## this would automatically ensure that the attributes firstname and lastname are stored always without preceeding and trailing spaces. thanks for your help…
> 
> Posted viahttp://www.ruby-forum.com/.

better no 404

[http://www.railsjitsu.com/writing-your-own-custom-validators](http://www.railsjitsu.com/writing-your-own-custom-validators)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 25, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/how-to-create-a-custom-validator/121160/4 "2007-11-25T15:30:48Z")

</div>

thanks … this one is exactly what i need.

> [http://www.railsjitsu.com/writing-your-own-custom-validators](http://www.railsjitsu.com/writing-your-own-custom-validators)
