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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:33:00+00:00 2026-05-26T07:33:00+00:00

I am trying to create a custom validation method for my date time field,

  • 0

I am trying to create a custom validation method for my date time field, and i follow example at here: Jquery validate date range fails and here: Validate that end date is greater than start date with jQuery

Following is my form ( in mvc 3 razor partial view ):

@using (Html.BeginForm("CreateFood", "Stock", FormMethod.Post, new { id = "formData" }))
{
       @Html.ValidationSummary(false, "Opps. Please correct the mistakes")
       <div class="editor-label">
           Storage Date
       </div>
       <div class="editor-field">
          @Html.EditorFor(model => model.StorageDate, new { @class = "storedate" })
          @Html.ValidationMessageFor(model => model.StorageDate)
       </div>

       <div class="editor-label">
          Expiry Date
       </div>
       <div class="editor-field">
         @Html.EditorFor(model => model.ExpiryDate, new { @class = "expirydate" })
         @Html.ValidationMessageFor(model => model.ExpiryDate)
       </div>
 }

And the script:

jQuery.validator.addMethod("dateRange", function () {
        var date1 = new Date(jQuery(".expirydate").val());
        alert(date1);
        var date2 = new Date(jQuery(".storedate").val());
        alert(date2);
        alert(date1 < date2);
        return (date1 < date2);
    }, "Please check your dates. The start date must be before the end date.");

$createdialog.dialog("option", "buttons", {
        "Cancel": function () {
            $createdialog.dialog('close');
        },
        "Submit": function () {

            // Validate the form before ask for cabinet
            var frm = $('#formData');

            var validator = frm.validate({
                rules: {
                    "ExpiryDate": { dateRange: true }
                }
            });
            if (frm.valid()) {
                 submitForm();
            }

Question:

  1. I am not really sure about the syntax and what should I put at “ExpiryDate” (is it a selector? or is the form field name? ):
    var validator = frm.validate({
    rules: {
    “ExpiryDate”: { dateRange: true }
    }

  2. All the alert() in the method doesnt show, so I assume the method is never fire.. Any idea??

(PS: I am using datepicker for both date field with specific format as well:

  $('.storedate').datepicker({ dateFormat: 'D, dd M yy' });
  $('.expirydate').datepicker({ dateFormat: 'D, dd M yy' });

)

Really need help here… already been searching methods for few days just to implement the datetime validation…

Appreciate any feedback… Thanks…
});

  • 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-26T07:33:01+00:00Added an answer on May 26, 2026 at 7:33 am

    These post series by Scott Allen, it may help you with a better approach.

    http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx
    http://odetocode.com/Blogs/scott/archive/2011/02/22/custom-data-annotation-validator-part-ii-client-code.aspx

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

Sidebar

Related Questions

I'm trying to create a method that will custom validate any TextBox control passed
I'm trying to extend ActiveRecord with the custom validation method validate_as_email so i could
I'm trying to create a custom validation wherein I have defined the annotation to
I am trying to create a custom field type in SharePoint. My custom field
I am trying to create a custom validator method for phone numbers. I would
I am trying to create a new custom validation in which I can compare
I'm trying to create a custom attribute validation for a webform projects. I already
I'm trying to write a custom validation method in my app - I have
I'm trying to create a simple custom validator for my project, and I can't
I'm trying to create a custom form element which extends Zend_Form_Element_Text with a validator

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.