I am trying to validate a dollar amount using a regex:
^[0-9]+\.[0-9]{2}$
This works fine, but whenever a user submits the form and the dollar amount ends in 0(zero), ruby(or rails?) chops the 0 off.
So 500.00 turns into 500.0 thus failing the regex validation.
Is there any way to make ruby/rails keep the format entered by the user, regardless of trailing zeros?
I presume your dollar amount is of decimal type. So, any value user enters in the field is being cast from string to appropriate type before saving to the database. Validation applies to the values already converted to numeric types, so regex is not really a suitable validation filter in your case.
You have couple of possibilities to solve this, though:
validates_numericality_of. That way you leave the conversion completely to Rails, and just check whether the amount is within a given range.validate_eachmethod and code your validation logic yourself (e.g. check whether the value has more than 2 decimal digits).So, in your case, you should be able to use:
Note, however, that users might find it tedious to follow your rigid entry rules (I would really prefer being able to type
500instead500.00, for example), and that in some locales period is not a decimal separator (if you ever plan to internationalize your app).