On Jul 21, 12:51 am, Pallavi Naik <[email protected]> wrote:
> Hi,
> #Validation on the credit_card field
> validates_format_of :credit_card,
> :with => /\A\d\d\d\d(-|\s)?\d\d\d\d(-|\s)?\d\d\d\d(-|\s)?\d\d\d\d\Z/,
> :on => :create,
> :message=>"Invalid credit card format"
Actually, here it is better to use [0-9] for number as \d allows plus
and minus signs as well as point.
The same for spaces it is better to use [ -] to allow only those
characters.
And not restricting the validation on create.
>
> # Strip everything but numbers
> def before_save
> self.credit_card = credit_card.gsub(/[^0-9]/, "") if
> attribute_present?("credit_card")
> end
Here, on the contrary it is better to use \d. And if you use
before_validation_on_create, and before_validation_on_update, it
should work.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
ruby-on-rails-programming-with-passion-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---