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

  • Home
  • SEARCH
  • 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 7414837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:10:47+00:00 2026-05-29T07:10:47+00:00

My Model fields accepting numbers and empty string : [DisplayName(Height)] [RegularExpression (@^\d*$, ErrorMessage=Height must

  • 0

My Model fields accepting numbers and empty string :

[DisplayName("Height")]
[RegularExpression (@"^\d*$", ErrorMessage="Height must be a number or left out   blank")]
public string Height { get; set; }

[DisplayName("Width")]
[RegularExpression(@"^\d*$", ErrorMessage = "Height must be a number or left out blank")]
public string Width { get; set; }

My view:

<%= Html.LabelFor(x => x.Width) %>:
<%= Html.TextBoxFor(x => x.Width) %>

<%= Html.LabelFor(x => x.Height) %>:
<%= Html.TextBoxFor(x => x.Height) %>

In controller action :

[HttpPost]

public ActionResult Edit(MyModeltype model)
{
    model.Width = String.IsNullOrEmpty(model.Width) ? "" : model.Width; //NEEDED?
    model.Height = String.IsNullOrEmpty(model.Height) ? "" : model.Height; //NEEDED?

    if (ModelState.IsValid)
        SaveSettings(model);

    return View("SomeView");
}

When I provide empty textboxes, Model.Width and .Height are passed as nulls and ModelState.IsValid is false. I just need to be able to pass the empty string. When omitting the regex attribute, same problem, so its not the regex. Thank you!

  • 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-29T07:10:48+00:00Added an answer on May 29, 2026 at 7:10 am

    Try adding the DisplayFormat attribute to your model’s properties:

    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [DisplayName("Height")]
    [RegularExpression (@"^\d*$", ErrorMessage="Height must be a number or left out   blank")]
    public string Height { get; set; }
    
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [DisplayName("Width")]
    [RegularExpression(@"^\d*$", ErrorMessage = "Height must be a number or left out blank")]
    public string Width { get; set; }
    

    This will ensure empty textbox values are not converted to NULL.

    Alternatively, you could explicitly define your getters and setters to convert nulls to empty strings:

    private string _width;
    public string Width
    {
        get { return _width ?? string.Empty; }
        set { _width = value ?? string.Empty; }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django model with a large number of fields and 20000+ table
Some model fields such as DateTimeField require null=True option when blank=True option is set.
Where should the validation of model fields go in django? I could name at
I have an ActiveRecord model whose fields mostly come from the database. There are
Is it possible to read a Django model's fields' options? For example, with the
I have a model with many fields (nearly 40). The client wants the fields
I want to have a model with calculated fields that I can apply sorting
my form is class MapForm(forms.ModelForm): class Meta: model = Map fields = ('mapName', 'kmlStr')
I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have a model that contains one-to-one fields to other models. I over rid

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.