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 7597749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:14:01+00:00 2026-05-30T22:14:01+00:00

I have this in my view model: [Required(ErrorMessage = Price is required)] [Range(0.01, 999999999,

  • 0

I have this in my view model:

[Required(ErrorMessage = "Price is required")]
[Range(0.01, 999999999, ErrorMessage = "Price must be greater than 0.00")]
[DisplayName("Price ($)")]
public decimal Price { get; set; }

I’d like to validate that the user doesn’t enter more than 2 decimal places. So I’d like to have

Valid values: 12, 12.3, 12.34

Invalid values: 12., 12.345

Is there a way to validate this with a data annotation?

  • 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-30T22:14:02+00:00Added an answer on May 30, 2026 at 10:14 pm

    You could use the RegularExpression attribute, with a regex that matches your criteria. There are a whole bunch of expressions here that involve numbers, I’m sure one will fit the bill. Here is the link.

    This will get you started, though it may not be as inclusive as you want (requires at least one digit leading the decimal point):

    [RegularExpression(@"\d+(\.\d{1,2})?", ErrorMessage = "Invalid price")]
    

    Note that it is difficult to emit a precise error message because you don’t know which part of the regex failed to match (the string “z.22” has the correct number of decimal places, for example, but is not a valid price).

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

Sidebar

Related Questions

Inside my Model I have this code: [Required(ErrorMessage = News content is required.)] [DisplayName(News
In the world of MVC I have this view model... public class MyViewModel{ [Required]
I have a view model that looks like this public class ViewModelRound2 { public
I have a view model that looks like this: public class VenueIndexViewModel : BaseViewModel
I have a basic MVC view model with annotations, for example: [Required(ErrorMessage=Your Name Required)]
I have this decimal set up: [Required(ErrorMessage = Please tell us your income)] [Display(Name
I have this field in my model. class AddUserModel { // ....other fields [Required(ErrorMessage
I have this view @model IEnumerable<ViewModelRound2> ... <snip> ... @{ using (Html.BeginForm(new { round1Ring3Bids
I have this attribute in my view model: [CustomRequired, EmailRegex] [Display(Name = KeepInformedPersonMail, ResourceType
I have code like this in my view model: function ChatListViewModel(chats) { var self

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.