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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:42:43+00:00 2026-06-01T16:42:43+00:00

I want to write a custom validator for MVC.NET framework that checks if entered

  • 0

I want to write a custom validator for MVC.NET framework that checks if entered date is in the future. To do it, I wrote the following class:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class InTheFutureAttribute : ValidationAttribute, IClientValidatable
    {
        private const string DefaultErrorMessage = "{0} should be date in the future";

        public InTheFutureAttribute()
            : base(DefaultErrorMessage)
        {
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format(ErrorMessageString, name);
        }

        public override bool IsValid(object value)
        {
            DateTime time = (DateTime)value;

            if (time < DateTime.Now)
            {
                return false;
            }

            return true;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var clientValidationRule = new ModelClientValidationRule()
            {
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
                ValidationType = "wrongvalue"
            };

            return new[] { clientValidationRule }; 
        }
    }

and added attribute to field that I want to check.

On the View page I create input field in the following way:

<div class="editor-label-search">
            @Html.LabelFor(model => model.checkIn)
        </div>
        <div class="editor-field-search-date">
            @Html.EditorFor(model => model.checkIn)

             <script type="text/javascript">
                $(document).ready(function ()
                { $('#checkIn').datepicker({ showOn: 'button', buttonImage: '/Content/images/calendar.gif', duration: 0, dateFormat: 'dd/mm/yy' }); });
            </script>  
            @Html.ValidationMessageFor(model => model.checkIn)
        </div>

When I submit the form for the controller that requires model with checked attribute code in my validator is called and it returns false, but instead of displaying an error it just call my controller’s action and send invalid model to it.

Am I doing something wrong? How can I fix 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-06-01T16:42:44+00:00Added an answer on June 1, 2026 at 4:42 pm

    Did you add the client-side validation logic: the custom validator and unobtrusive adapter.
    If not, here is the client side logic:

    //Add the client-side validator:

    jQuery.validator.addMethod("wrongvalue",
     function (value, element, param) {
    
        var dateValue = $.datepicker.parseDate('dd/mm/yy', value); //use any date parser, if UI/Datepicker is not used
    
        var now = new Date();
        var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    
        var isValid = dateValue >= today;
        return isValid;
    });
    

    //Add an unobtrusive adapter:

    jQuery.validator.unobtrusive.adapters.add("wrongvalue", [], function (options) {
    
        options.rules["wrongvalue"] = true;
        options.messages["wrongvalue"] = options.message;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I'm developing application using asp.net and I want to write Custom validator which
I want to modify the custom validator below so it checks that the RadEditor
I want to write a custom class that behaves like dict - so, I
I want to write a custom validator which doesn't do anything before the user
as question says, i want to write custom data type data of a class
I want to write a custom control that's used like this: <HorizontalTick>Some string</HorizontalTick> It
I want to write a custom HTTP Handler in ASP.Net (I'm using C# currently)
I have a requirement that calls for a custom validator. The users want a
I have a custom asp.net mvc class validation attribute. My question is how can
I want to write a custom view engine that returns custom text (like coma

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.