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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:59:01+00:00 2026-05-16T14:59:01+00:00

What approach do you recommend for validating a DateTime on the client side in

  • 0

What approach do you recommend for validating a DateTime on the client side in MVC?

Let’s say I have a model with a property named DateOfBirth that is a DateTime, like so.

public class UserModel
{
    [DataType(DataType.Date)]
    public DateTime DateOfBirth {get;set;}
}

On the View, I have a simple

<%: Html.LabelFor(model=>model.DateOfBirth) %>
<%: Html.EditorFor(model=>model.DateOfBirth) %>
<%: Html.ValidationMessageFor(model=>model.DateOfBirth) %>
<input type="submit" value="Submit" />

I can use either the Microsoft MVC validations or the jQuery validations. How do I get the DateTime to validate client-side?

I realize all the DataTypeAttribute does is provide formatting hints and doesn’t really do any validation (it leaves that part to the ModelBinder).

Basically I want to duplicate what the ModelBinder does when it tries to put the posted value into the model’s DateOfBirth property.

What are your recommendations?

  • 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-16T14:59:02+00:00Added an answer on May 16, 2026 at 2:59 pm

    As suggested above, follow Phil Haack’s post on custom validations: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

    Here’s how I would do it:


    1. Add a “DateFormatAttribute” class, like so:
    
        public class DateFormatAttribute : ValidationAttribute {
          public override bool IsValid(object value) {    
            if (value == null) {
              return true;
            }
    
            // Note: the actual server side validation still has to be implemented :-)
            // Just returning true now...
    
            return true;
          }
        }
    

    1. Add a “DateFormatValidator” class, like so:
    
        public class DateFormatValidator : DataAnnotationsModelValidator 
        {
          string message;
    
          public PriceValidator(ModelMetadata metadata, ControllerContext context
            , DateFormatAttribute attribute)
            : base(metadata, context, attribute) 
          {
            message = attribute.ErrorMessage;
          }
    
          public override IEnumerable GetClientValidationRules() 
          {
            var rule = new ModelClientValidationRule {
              ErrorMessage = message,
              ValidationType = "date" // note that this string will link to the JavaScript function we'll write later on
            };
    
            return new[] { rule };
          }
        }
    

    1. Register the above classes somewhere in Global.asax.cs:
    
        DataAnnotationsModelValidatorProvider
            .RegisterAdapter(typeof(DateFormatAttribute), typeof(DateFormatValidator));
    

    1. Add a validation function on the client. Note that this will have to be implemented attributng the locale of the user. The following is a Dutch (nl-NL, nl-BE) client-side validation function:
    
        /*
         * Localized default methods for the jQuery validation plugin.
         * Locale: NL
         */
        jQuery.extend(jQuery.validator.methods, {
            date: function(value, element) {
                return this.optional(element) || /^\d\d?[\.\/-]\d\d?[\.\/-]\d\d\d?\d?$/.test(value);
            }
        });
    

    That should cover things…

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

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The default implementation of GetHashCode() is based on the reference,… May 16, 2026 at 3:58 pm
  • Editorial Team
    Editorial Team added an answer The presence of secondary case class constructors don't cause the… May 16, 2026 at 3:58 pm
  • Editorial Team
    Editorial Team added an answer class rand_number { //... if(rand_n == 2) { boolean guessed… May 16, 2026 at 3:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

What logging library or approach would you recommend for this case: We want to
i have searched the web relentlessly for this and have not found anything -
I want to create a Rails (2.1 and 2.2) model with ActiveRecord validations, but
I have created an XML schema (foo.xsd) and used xjc to create my binding
I am developing an iPhone application that persists data to a SQLite3 database. For
We have a flash gateway page displayed to users (only once) before they enter
Can anyone recommend a fast way to sort the contents of a text file,
How can I use HTTP basic Authentication and have the user submit their Username
I read an answer to a question on Stack Overflow that contained the following
Many pages have a search box to them which will typically have the overlayed

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.