Google Groups Home
Help | Sign in
Message from discussion Cleaning Currency Values
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Anthony Carlos  
View profile
 More options Jan 15, 12:56 am
From: Anthony Carlos <anth...@digitalphenom.com>
Date: Tue, 15 Jan 2008 00:56:06 -0500
Local: Tues, Jan 15 2008 12:56 am
Subject: Re: [CRB] Cleaning Currency Values

Hey Bryce,

I would use a regex to strip out anything that is not a digit or a  
decimal point:

input.gsub(/[^0-9.]/, '').to_f

I'm not sure of the "Rails Way," but it seems like a before filter  
might work well.

-Anthony

On Jan 15, 2008, at 12:18 AM, Bryce Thornton wrote:

> I'm trying to figure out how to clean up some form input for a
> currency value.  I'm using a decimal column to store the value and
> have a basic text input box for the data entry.

> My question: how can I sanitize this input data?

> For instance, say the user enters "$115.25".  Currently this is being
> stored as "0.00" since ruby detects the dollar sign and strips
> everything since it's not a numerical value.  Similarly, "15,000.00"
> is stored as "15.00" since the comma is detected as a non-numerical
> value. It does this as soon as I call "update_attributes" in the
> controller, so I don't think I can do much about in the model. By the
> time before_validation fires it's already too late.  I would assume
> the same problem exists for ruby Integers, Floats, etc.  So, what's
> the "Rails Way" of handling this?  Is there a way to clean the
> parameters in the model before rails actually sets each attribute or
> am I going to have to handle this in the controller?

--

Anthony Carlos
Vice President
Digital Phenom
807 National Press Bldg
Washington DC 20045
202.393.0000


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google