Forum: Ruby on Rails Either/Or Validation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Joshua S. (Guest)
on 2006-01-30 23:20
How would I use validations to ensure I either received a blank value or
a ten digit number?

If I set validates_length_of :field, :in => 0..10 then that would allow
any value in-between.  The allow_nil => true option does not seem to

I've also tried using this with some combination of
validates_numericality_of but cannot get it to work.

Any help would be greatly appreciated.

Dave T. (Guest)
on 2006-01-31 01:03
(Received via mailing list)
There might be a some funky validates_* methods I don't know of, but
typically I define my own validate method whenever I need to do anything
more than basic validations.

To do this, just add a validate method to your AR class; here's a
snippet from my site:

def validate
  is_taken = Member.find_by_email(

  unless (is_taken.nil? || ==
    errors.add_to_base("The email address <b>#{}</b> has
already been registered on DevLists.")


On Monday, January 30, 2006, at 10:20 PM, Joshua S. wrote:
>Any help would be greatly appreciated.
>Posted via
>Rails mailing list

This topic is locked and can not be replied to.