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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:39:42+00:00 2026-06-14T17:39:42+00:00

I am currently using MVC Data Annotations to perform validation on my model. [MinLength(4,

  • 0

I am currently using MVC Data Annotations to perform validation on my model.

[MinLength(4, ErrorMessage = "The {0} must be at least {2} characters long")]
[MaxLength(16, ErrorMessage = "The {0} must be {2} characters long or less")] 
[DataType(DataType.Password)]
[Display(Name = "New Password")]
public string Password { get; set; }

However, I’m stuck dealing with a field that is not required, but needs to have a MinLength when there is something in the input field. Simply removing

[Required]

does not help. Is there any way to do this without creating yet another custom validation attribute?

  • 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-14T17:39:43+00:00Added an answer on June 14, 2026 at 5:39 pm

    Seems like your property has empty or white space string value, because MinLength attribute considers null as a valid value:

    public override bool IsValid(object value)
    {
        this.EnsureLegalLengths();
        int length = 0;
        if (value == null) 
        {
            return true; // <-- null is valid!
        }
        string str = value as string;
        if (str != null)
        {
            length = str.Length;
        }
        else
        {
            length = ((Array) value).Length;
        }
        return (length >= this.Length);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been using data annotations for my validation in an MVC project
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to
I'm currently migrating a project that is using Spring MVC without annotations to Spring
i am using .net 4 with mvc 2 and annotations for validation. I built
I'm using the MVC model but I cannot get the data needed for the
I am currently building an application using ASP.NET MVC. The data entry pages are
I am currently using MVC 1.0 and .NET 3.5. I am using DataAnnotations to
I am currently using asp.NET MVC to build a Content Management System parts of
I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.
Hi I am currently using the asp.net MVC 4 rc with System.Web.Optimization. Since my

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.