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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:53:39+00:00 2026-05-24T12:53:39+00:00

I have a DateTime TemplateEditor and I would like to add regex validation to

  • 0

I have a DateTime TemplateEditor and I would like to add regex validation to it. I have a RegularExpression attribute that I could decorate the model with, but I dont want to have to decorate every datetime property in all my models with a regex.

Is there way I can my custom TemplateEditor add the appropriate unobstrusive tags when it renders the textbox for it?

  • 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-24T12:53:42+00:00Added an answer on May 24, 2026 at 12:53 pm

    Instead of adding your validator in the template, you should insert your validator using a custom ModelMetadataValidatorProvider. First, create your ModelMetadataProvider class:

    public class MyModelMetadataValidatorProvider : DataAnnotationsModelValidatorProvider
    {
    
        internal static DataAnnotationsModelValidationFactory DefaultAttributeFactory = Create;
        internal static Dictionary<Type, DataAnnotationsModelValidationFactory> AttributeFactories = new Dictionary<Type, DataAnnotationsModelValidationFactory>() {
            {
                typeof(RegularExpressionAttribute),
                (metadata, context, attribute) => new RegularExpressionAttributeAdapter(metadata, context, (RegularExpressionAttribute)attribute)
            }
        };
    
        internal static ModelValidator Create(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
        {
            return new DataAnnotationsModelValidator(metadata, context, attribute);
        }
    
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            List<ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList();
    
            // inject our new validator
            if (metadata.ModelType.Name == "DateTime")
            {
                DataAnnotationsModelValidationFactory factory;
    
                RegularExpressionAttribute regex = new RegularExpressionAttribute(
                    "^(((0?[1-9]|1[012])/(0?[1-9]|1\\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\\d)\\d{2}|0?2/29/((19|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$");
                regex.ErrorMessage = "Invalid date format";
                if (!AttributeFactories.TryGetValue(regex.GetType(), out factory))
                    factory = DefaultAttributeFactory;
    
                vals.Add(factory(metadata, context, regex));
            }
    
            return vals.AsEnumerable();
        }
    }
    

    Next, register your ModelMetadataValidatorProvider in Global.asax.cs in Application_Start.

        ModelValidatorProviders.Providers.Clear();
        ModelValidatorProviders.Providers.Add(new MyModelMetadataValidatorProvider());
    

    Now, when you access a model, a RegularExpressionAttribte will be attached to each DateTime field. You can also extend this to provide a localized DateTime regular expression and message.

    counsellorben

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

Sidebar

Related Questions

I have a DATETIME field. I would like to select all the records that
I have a DateTime? field. Its currently storing UT. I would like it to
I have a datetime column in SQL Server that gives me data like this
i have a DateTime Field that must be shown in Edit view @Html.EditorFor( model
I have datetime strings that look can like any the following: 1 13 2009
I have two Datetime fields that I wish to add together. They are in
I have a DateTime object that I need to print in a custom gridlike
I have an datetime object that I want to remove one hour to display
I have a datetime column in db that I want to transform into a
I have a DATETIME for an entry in my database that is upcoming. I

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.