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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:21:52+00:00 2026-05-19T22:21:52+00:00

In an Asp.net MVC app, I have inherited this problem (if it is a

  • 0

In an Asp.net MVC app, I have inherited this problem (if it is a problem?) where one developer has used String for Date type.

In my model the property reads:

[Required]
[DisplayName("Registration Date")]
public string Registrationdate { get; set; }

The business requirement is that the field is not required, but if there is something in that fields then it must be a valid date.

How would you implement this requirement, without changing the data type?

  • 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-19T22:21:53+00:00Added an answer on May 19, 2026 at 10:21 pm

    It looks like you’re using System.ComponentModel.DataAnnotations. The best way to do this using this library would be to create a new attribute to validate date strings and apply it to the property. Here’s some code for you to start with:

    [AttributeUsage(AttributeTargets.Property, Inherited = true)]
    class DateAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var dateString = value as string;
            if (string.IsNullOrWhiteSpace(dateString))
            {
                return true; // Not our problem
            }
            DateTime result;
            var success = DateTime.TryParse(dateString, out result);
            return success;
        }
    }
    

    You’ll probably want to expand on this code depending on what kind of strings you’re expecting from the client. Also, this won’t give you any client-side validation.

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

Sidebar

Related Questions

I have an ASP.NET MVC app with a form. Say this form has an
I have implemented Membership Provider in my ASP.NET MVC app. Membership provider has the
I'm creating an asp.net MVC app, first time I've done this. I have a
I am building my first ASP.Net MVC based app and have a problem accessing
I have an asp.net mvc app which has membership implemented. So a user has
Writing an asp.net mvc app and i have something like this... Public Class AA
In my ASP.net MVC App (using Razor views) I have a ProductDetails view. This
In my ASP.Net MVC app, I have the following controllers HomeController ExController ExController has
I have 3 tables in my database like this: In my ASP.NET Mvc app.
Using Unity in an ASP.Net MVC 2 app I have various dependencies on Controllers

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.