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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:21:57+00:00 2026-06-11T14:21:57+00:00

I’m trying to implement a custom client side validation, but it is not working.

  • 0

I’m trying to implement a custom client side validation, but it is not working. I’m basing myself on the article on Codeproject http://www.codeproject.com/Articles/275056/Custom-Client-Side-Validation-in-ASP-NET-MVC3

I also looked here on SO, but I think I’m implementing it in the correct manner, but I’m overlooking something.
My goal is to validate a date (required, date format and not earlier than another date on the form). The first two can be done with data annotations, the last I have to do with custom validation.

I have on my base class some dataannotations (ClassLibrary is in VB.NET):

Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations

<MetadataType(GetType(CM_CONTRACTVALIDATIONData))>
Partial Public Class CM_CONTRACTACTIVATION
    '...
End Class

Public Class CM_CONTRACTVALIDATIONdata
'...
<DataType(DataType.Date)>
<Required()>
Public Property TakeBackDeviceWhen
'..
End Class

In the javascript file I have added the custom method:

//validation
$.validator.addMethod("checkPickupDate", function (value, element) {
    return false;
});
$("#form").validate({
    rules: {
        TakeBackDeviceWhen: {
            checkPickupDate: true
        }
    },
    messages: {
        TakeBackDeviceWhen: {
            checkPickupDate: "Test"
        }
    }
}
);

My chtml file is as follow:

@Html.TextBox("TakeBackDeviceWhen", Model.TakeBackDeviceWhen.HasValue ? Model.TakeBackDeviceWhen.Value.ToShortDateString() : "", new { style = "Width: 200px" })

The resulting HTML is as follow:

<input id="TakeBackDeviceWhen" class="hasDatepicker" type="text" value="" style="Width: 200px" name="TakeBackDeviceWhen" data-val-required="The TakeBackDeviceWhen field is required." data-val="true">

It seems that neither my type validation and my custom validation isn’t implemented.

What is going wrong?

  • 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-11T14:21:58+00:00Added an answer on June 11, 2026 at 2:21 pm

    OK, solved it. I hope 🙂

    What did I learned today:
    (1) Don’t use EditorFor: when you scaffold it from a MVC template, input fields are generated to EditorFor, it seems that you can’t add custom unobtrusive validation tags. So, I was trying to get this fixed, untill I changed it to TextBoxFor.

    (2) You can add custom validation methods in jQuery, but you can’t mix them with unobtrusive validation. After adding a custom method, you have to also add it to the unobtrusive adapters. And don’t forget to add jQuery on the bottom :-s (I got this from jQuery.validator.unobtrusive.adapters.addMinMax round trips, doesn't work in MVC3)

    $(function () {
    $.validator.addMethod("checkpickupdate", function (value, element) {
        if (value == "20/09/2012") {
            return false;
        } else {
            return true;
        }
    });
    
    $.validator.unobtrusive.adapters.addBool("checkpickupdate");
    } (jQuery));
    

    (3) Add validation tags to the input field in the htmlAttributes:

    @Html.TextBox("TakeBackDeviceWhen", Model.TakeBackDeviceWhen.HasValue ? Model.TakeBackDeviceWhen.Value.ToShortDateString() : "",
                    new { 
                        style = "Width: 200px", 
                        data_val = "true", 
                        data_val_required = "verplicht!",
                        data_val_date = "moet datum zijn",
                        data_val_checkpickupdate = "wow"
                     })
    

    (4) Datatype data annotations will not enforce a validation. You have to add it like in (3). You can add a custom ValidationAttribute like (for server side validation):

    public class MustBeDateAttribute : ValidationAttribute {
        public override bool IsValid(object value) {
            try
            {
                DateTime dte = DateTime.Parse(value.ToString());
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
    }
    

    And this is the resulting html output:

    <input type="text" value="" style="Width: 200px" name="TakeBackDeviceWhen" id="TakeBackDeviceWhen" data-val-required="required!" data-val-date="has to be a date" data-val-checkpickupdate="custom error" data-val="true" class="hasDatepicker valid">
    

    As I’m using my ClassLibrary in different projects, I’m now going to try to seperate the dataannotations meta data from the class library (maybe with dependency resolver).

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

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.