Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 765851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:57:08+00:00 2026-05-14T16:57:08+00:00

I am trying to validate a dollar amount using a regex: ^[0-9]+\.[0-9]{2}$ This works

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:57:09+00:00Added an answer on May 14, 2026 at 4:57 pm

    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:

    1. Use validates_numericality_of. That way you leave the conversion completely to Rails, and just check whether the amount is within a given range.
    2. Use validate_each method and code your validation logic yourself (e.g. check whether the value has more than 2 decimal digits).
    3. Validate the attribute before it’s been typecasted:

    This is especially useful in
    validation situations where the user
    might supply a string for an integer
    field and you want to display the
    original string back in an error
    message. Accessing the attribute
    normally would typecast the string to
    0, which isn‘t what you want.

    So, in your case, you should be able to use:

    validates_format_of :amount_before_type_cast, :with => /^[0-9]+\.[0-9]{2}$/, :message => "must contain dollars and cents, seperated by a period"
    

    Note, however, that users might find it tedious to follow your rigid entry rules (I would really prefer being able to type 500 instead 500.00, for example), and that in some locales period is not a decimal separator (if you ever plan to internationalize your app).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to validate a form server side, the way it works
I am trying to validate a form, such that if IP of user (
I was trying to validate my User model data and I came upon this
I'm trying to validate a form, but doesn't work :\ , When I submit
I am trying to validate the form input but my validation is not working.
I'm trying to validate my registration form. I'm using djanga-registration, it already has password
I am trying to validate a form, using jQuery validate and show errors messages
I am trying to validate user id's matching the example: smith.jack or smith.jack.s In
I'm trying to put together a comprehensive regex to validate phone numbers. Ideally it
I'm trying to validate an XML file against the schemas it references. (Using Delphi

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.