forms - Can't validate phone number -


i'm trying validate phone number without success. when submit form following model, accepts phone number put in, whether it's valid or not. why happening?

class client < activerecord::base   belongs_to :salon   belongs_to :address   accepts_nested_attributes_for :address   attr_accessible :address_attributes, :name, :phone, :email   validates_presence_of :name   validates_presence_of :email   validates_presence_of :phone,     :unless => proc.new { |c| c.phone.gsub(/[^0-9]/, "").length != 10 } end 

-

<%= form_for(@client) |f| %>   <% if @client.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@client.errors.count, "error") %> prohibited client being saved:</h2>        <ul>       <% @client.errors.full_messages.each |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>    <%= f.hidden_field :salon_id, :value => salon.logged_in_salon.id %>   <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :phone %><br />     <%= f.text_field :phone %>   </div>   <div class="field">     <%= f.label :email %><br />     <%= f.text_field :email %>   </div>    <%= f.fields_for :address |address_form| %>     <div class="field">       <%= address_form.label :line1 %><br />       <%= address_form.text_field :line1 %>     </div>     <div class="field">       <%= address_form.label :line2 %><br />       <%= address_form.text_field :line2 %>     </div>     <div class="field">       <%= address_form.label :city %><br />       <%= address_form.text_field :city %>     </div>     <div class="field">       <%= address_form.label :state_id %><br />       <%= select("client[address]", "state_id", state.all.collect {|s| [ s.name, s.id ] }) %>     </div>     <div class="field">       <%= address_form.label :zip %><br />       <%= address_form.text_field :zip %>     </div>   <% end %>    <div class="actions">     <%= f.submit %>   </div> <% end %> 

you should using validates_format_of instead of validates_presence_of. like:

validates_format_of :phone,                      :with => /\a[0-9]{10}\z/,                      :allow_blank => true,                      :allow_nil => true 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -