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-9_]@[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 via http://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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tushar Gandhi 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-9_]@[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, > TusharHave you considered :unless -- Posted via http://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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
:allow_nil => true On Apr 16, 4:42 pm, Tushar Gandhi <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-9_]@[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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, Use allow_blank => true -- Posted via http://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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
validates_format_of :email, :with=>/^[0-9a-zA-Z_][\w\.-]*[a-zA-Z0-9_]@[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-9_]@[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...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-9_]@[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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.