Hi all, I''ve got a form which accepts a "price" field and I''m attempting to validate it and allow some flexibility in the way the user enters it. It''s a numeric field but I want the user to be able to enter dollar signs and commas naturally in the data entry. I''m trying to understand how to use validates_format_of, and I''m not sure what I''m missing. I''m a beginner at regular expressions but I''ve been researching it. I tried an extremely simple case: validates_format_of :price, :with => /^\d$/ If I understand correctly, this validation should only succeed for a single digit character and nothing else? I added this to my model and so far it passes everything--numbers, characters, whitespace, etc. What am I doing wrong? I figure I''ll get this simplest case working and move on from there. Thanks. Jeff Coleman -- Posted via http://www.ruby-forum.com/.
Jeff Coleman wrote:> > Hi all, > > I''ve got a form which accepts a "price" field and I''m attempting to > validate it and allow some flexibility in the way the user enters it. > It''s a numeric field but I want the user to be able to enter dollar > signs and commas naturally in the data entry. > > I''m trying to understand how to use validates_format_of, and I''m not > sure what I''m missing. I''m a beginner at regular expressions but I''ve > been researching it. > > I tried an extremely simple case: > > validates_format_of :price, :with => /^\d$/ > > If I understand correctly, this validation should only succeed for a > single digit character and nothing else? I added this to my model and > so far it passes everything--numbers, characters, whitespace, etc. > > What am I doing wrong? I figure I''ll get this simplest case working and > move on from there.Just wanted to see if I''m doing something wrong here or not. I''d really like to get validates_format_of to validate a string value before I need to turn it into an integer. Is that possible? I was able to get predictable results when applying validates_format_of to a model attribute which is a string, but it doesn''t seem to work when applying to an integer value. What''s the best way to allow the user to enter something like "$500,000" and turn it into "500000"? Thanks, Jeff Coleman -- Posted via http://www.ruby-forum.com/.
Jeff Coleman wrote:> What''s the best way to allow the user to enter something like "$500,000" > and turn it into "500000"?If you want to allow oddball data entry but clean it up as best as possible before validation, declare a "before_validation" method on the model, which rails will automatically call: def before_validation money_amount = money_amount.gsub!(/[^\d\.]/, "").to_i end this method will strip money_amount of everything except numerals and decimal points, then convert it to an integer. this is more polite to your user than just validates_format_of, since it tries to fix their input rather than just complain about it. you should still run validations on the results, of course. - michael -- Posted via http://www.ruby-forum.com/.
whoops, should be: def before_validation money_amount = money_amount.gsub(/[^\d\.]/, "").to_i end -- Posted via http://www.ruby-forum.com/.