# Validates\_format\_of?

**URL:** <https://www.ruby-forum.com/t/validates-format-of/93611>\
**Category:** Rails\
**Created:** [April 5, 2006, 3:46am UTC](https://www.ruby-forum.com/t/validates-format-of/93611 "2006-04-05T03:46:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 5, 2006, 3:46am UTC](https://www.ruby-forum.com/t/validates-format-of/93611/1 "2006-04-05T03:46:41Z")

</div>

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 C.man

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 5, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/validates-format-of/93611/2 "2006-04-05T17:15:52Z")

</div>

Jeff C.man 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 C.man

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 5, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/validates-format-of/93611/3 "2006-04-05T18:00:26Z")

</div>

Jeff C.man 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 5, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/validates-format-of/93611/4 "2006-04-05T19:13:35Z")

</div>

michael lascarides wrote:

> whoops, should be:
> 
> def before\_validation  
> money\_amount = money\_amount.gsub(/[^\d.]/, “”).to\_i  
> end

Thanks, Michael!

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 5, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/validates-format-of/93611/5 "2006-04-05T18:01:43Z")

</div>

whoops, should be:

def before\_validation  
money\_amount = money\_amount.gsub(/[^\d.]/, “”).to\_i  
end
