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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:36:28+00:00 2026-05-27T00:36:28+00:00

We have an attribute called DateReleased for which the following Data Annotation attributes are

  • 0

We have an attribute called DateReleased for which the following Data Annotation attributes are added

[Required]
[DataType(DataType.Date, ErrorMessage = "Please enter date")]
[DisplayName("Date Released")]
public object DateReleased { get; set; }

Following is the Action which is implemented to insert a new record to the database

[HttpPost]
public ActionResult Create([Bind(Exclude="Id")] Movie movie)
{
    try
    {
        if (ModelState.IsValid)
        {
            _entities.AddToMovies(movie);
            _entities.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(movie);
    }
    catch
    {
        return View();
    }
}

I have enabled the client validation, by placing the following lines of code in the create view

<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcValidation.js"></script>

<% Html.EnableClientValidation(); %>

But I was surprised to find that only Required validation is firing on client side. Data Type validation of Date is only firing at server side. Please let me know the reason behind the failure of client side validation and what would be workarounds to fire the client side validation.

  • 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-27T00:36:29+00:00Added an answer on May 27, 2026 at 12:36 am

    Yes. Add a custom attribute class as below

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class DateAttribute : DataTypeAttribute
    {
        public DateAttribute() : base(DataType.Date) { }
    
        public override string FormatErrorMessage(string name)
        {
            if (ErrorMessage == null && ErrorMessageResourceName == null)
            {
                ErrorMessage = ValidatorResources.DateAttribute_Invalid;
            }
            return base.FormatErrorMessage(name);
        }
    
        public override bool IsValid(object value)
        {
            if (value == null) return true;
            DateTime retDate;
            return DateTime.TryParse(Convert.ToString(value), out retDate);
        }
    }
    

    Create Client Validation Rule class

    public class ModelClientValidationDateRule:ModelClientValidationRule
    {
        public ModelClientValidationDateRule(string errorMessage)
        {
            ErrorMessage = errorMessage;
            ValidationType = "date";
        }
    }
    

    Create an adapter class which hooks in the custom attribute and client validation rule as below. Make sure of adding refernce of the above attribute class

    public class DateAttributeAdapter : DataAnnotationsModelValidator<DateAttribute>
    {
        public DateAttributeAdapter(ModelMetadata metadata, ControllerContext context, DateAttribute attribute)
            : base(metadata, context, attribute) { }
    
        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            return new[] { new ModelClientValidationDateRule(ErrorMessage) };
        }
    }
    

    Then modify the global.asax file

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterRoutes(RouteTable.Routes);
    
        DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateAttribute), typeof(DateAttributeAdapter));
    }
    

    Adding the Attribute to the model class as below

    [Date]
    public object DateReleased { get; set; }
    

    Adding following client side code in the view

    <script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.validate.js"></script>
    <script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>
    <script type="text/javascript" src="../../Scripts/MicrosoftMvcValidation.js"></script>
    <script type="text/javascript">
    
    Sys.Mvc.ValidatorRegistry.validators["date"] = function (rule) {
        // initialization code can go here.
        return function (value, context) {
            if (value.length > 0) {
                var d = new Date(value);
                if (!isNaN(d))
                    return true;
                return rule.ErrorMessage;
            }
            else {
                return true;
            }
        };
    };
    

    Hope this would help you.

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

Sidebar

Related Questions

I have an abstract entity called Block which contains two attributes: column and order
I have a attribute called: description and I want to have the following in
So, I have a custom attribute called CompressAttribute which is set up as a
I have an array(sorted on an attribute called node_id) of ruby objects which basically
I have a Rails Model called Events which has as field/attribute called :description. I
I have a simple core-data entity that has a Boolean attribute called subscribedToNewsletter .
I have two tables job which contains an attribute called employer_id_job that links it
I have block of data sample in XML. Each data has a attribute called
I have a coredata attribute called visitDate I want to set the date and
I have a Model called Todo.rb which has an attribute called asset which is

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.