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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:31:53+00:00 2026-06-18T05:31:53+00:00

I have a model with number of validation rule: [Required(ErrorMessage = Number must be

  • 0

I have a model with number of validation rule:

[Required(ErrorMessage = "Number must be enter")]
    [RegularExpression("^[0-9]{1,10}$", ErrorMessage = "Your must be enter only integer number between 0-10 simvols")]
    public int Number
    {
        get { return number; }
        set { number = value; }
    }

so my validation does not work.I check my code, but can not find where I did wrong.
This is part of my view template:

<% using (Html.BeginForm())
   {%>
<%: Html.ValidationSummary(true) %>
<div class="editor-label">
    <p class="number">
        Enter number</p>
    <%: Html.TextBoxFor(model => model.Number, new {@class = "txtNumber"})%>
    <%: Html.ValidationMessageFor(model => model.Number) %>
</div>
<p>
    <input type="submit" value="Calculate" class="button" />
</p>
<% } %>
  • 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-06-18T05:31:54+00:00Added an answer on June 18, 2026 at 5:31 am

    Your regular expression should be this:

    "^([0-9]|10)$"
    

    EDIT:

    Your real problem, however is that you aren’t doing things correctly. You are using an int type in your model. But then you’re using FormCollection in your Post action. FormCollection bypasses the model binder, and gives you whatever is typed in.

    You should instead be model binding your value, and you should be making it a nullable int in your model. The reason is that int is a value type, and cannot be null. Therefore, it must always contain a value, which in the case of you entering text means that value will be 0, and since 0 passes your validation, the ModelState.IsValid will return true.

    Instead, do this:

    [Required(ErrorMessage = "Number must be enter")]
    [Range"^[0-9]{1,10}$", 
        ErrorMessage = "Your must be enter only integer number between 0-10 simvols")]
    public int? Number {get;set;}
    

    Then in your action method:

    public ActionResult Index(MyModel model) {
        if (ModelState.IsValid) {
           // model.Number is now an integer already, and IsValid will be false for no number
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following validation rule in my model: 'dob' => array( 'required' =>
I have a model with a number of validation rules, all of which are
In model I have integer field. Meaning of value of this field is number
I have an IEnumerable containing strings, using Data Annotations for validation: [Required(ErrorMessage = This
I have the following property in my model: [Required] [Range(1, int.MaxValue, ErrorMessage = Bad
I have a .Net MVC4 model / view with a number of [Required] fields,
Imagine you have two fields in your Model: public class MyModel { [Required(ErrorMessageResourceType =
I have a model that has an arbitrary number of children entities. For simplicity
I have two arraylist with a number of model objects.I want to find the
I have a model object which has entities annotated with hibernate validation annotations. For

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.