validates_format_of :email,
:with=>/^[0-9a-za-z_][\w\.-]*[a-za-z0-...@[a-za-z0-9][\w\.-]*[a-za-
Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/,
                      :message => "is not valid",
:unless => Proc.new { |your_model_name_or_any_variable_name_you_want|
your_model_name_or_any_variable_name_you_want.email.blank?}

So, supposing your model is a User:

validates_format_of :email,
:with=>/^[0-9a-za-z_][\w\.-]*[a-za-z0-...@[a-za-z0-9][\w\.-]*[a-za-
Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/,
                      :message => "is not valid",
:unless => Proc.new {|user| user.email.blank?}


On Apr 16, 10:42 am, Tushar Gandhi <li...@ruby-forum.com> wrote:
> Hi,
> I am trying to validate the email format by using "validates_format_of".
> I have written a code like this:-
> "validates_format_of :email,
> :with=>/^[0-9a-za-z_][\w\.-]*[a-za-z0-...@[a-za-z0-9][\w\.-]*[a-za-z0-9]\.[a-za-z][a-za-z\.]*[a-za-z]$/,
>                       :message => "is not valid" "
>
> It is working fine, but in my application email id is not manadatory.
> So how should add the condition in this validator so that it will
> validate the email only when the email address is present.
>
> Can anyone help me out?
>
> Thanks,
> Tushar
> --
> Posted viahttp://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-t...@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to