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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:25:38+00:00 2026-06-04T10:25:38+00:00

Is there any way to trigger a specific jquery-unobtrusive rule on one field, when

  • 0

Is there any way to trigger a specific jquery-unobtrusive rule on one field, when the value in another field changes?

I have a form with two date fields (say start/end) on it, with validation that end must be greater than start. This works fine in the simple case of changing end after start is already set. What it doesn’t do is allow for is:

  • setting end first and then setting start
  • changing start after both have already been set, and violating the constraint

Server side validation will catch it of course, but it looks bad to have the error on end stay set even after you have fixed start, or no error showing up when the value in end is changed to an invalid value. The reason to trigger a specific rule is that I don’t want to fire off the required or date format rules on the same field before the user has had a chance to enter a value. The form starts off ‘clean’. However, if that isn’t possible then firing all the rules is ok.

Sorry for no code samples, but I don’t even know where to start with this.

Update:

What i’ve done for the moment is to dig around in the model (since this is an asp.net mvc project), find the attribute, and read it’s properties directly.

var controllerCtx = ViewContext.Controller.ControllerContext;
var da = ViewData.ModelMetadata.GetValidators(controllerCtx)
            .SelectMany(x => x.GetClientValidationRules())
            .Where(x => x.ValidationType == "isdateafter")
            .FirstOrDefault();

var otherid = da == null ? "" : da.ValidationParameters["propertytested"];

Then in the normal HTML part, I do a test on the start and see if it is a date picker, then wire up a basic check, and fire off all validation rules. Since there aren’t many rules, I just check to see if there is a value in the end field before running them. I’d like to use the ingenious solution below, and will give it a go when I have a bit of free time this week.

@if (otherid != "") {
    <text>
    var other = $("#@otherid");
    if (other && other.hasClass('hasDatepicker')) { // if the other box is a date/time picker
       other.datetimepicker('option', 'onSelect', function(dateText, instance) {
           var lowerTime = $(this).datetimepicker('getDate');
           $("#@id").datetimepicker('option', 'minDate', new Date(lowerTime.getTime()));
           if ($("#@id").val()) { // if there is a value in the other
                $('form').data('validator').element('#@id');
           }
        });
    }
    </text>
}
  • 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-04T10:25:39+00:00Added an answer on June 4, 2026 at 10:25 am

    This might work for you…

    $('form').data('validator').element('#Key')
    

    This grabs the validator off of your form, and forces validation on an individual item.

    http://docs.jquery.com/Plugins/Validation/Validator/element#element

    UPDATE

    See if this continues to help!

    $.extend($.validator.prototype, {
            elementWithRule: function(element, rule) {
                element = this.clean(element);
                this.lastElement = element;
                this.prepareElement(element);
                this.currentElements = $(element);
                var result = this.checkSpecificRule(element, rule);
                if (result) {
                    delete this.invalid[element.name];
                } else {
                    this.invalid[element.name] = true;
                }
                if (!this.numberOfInvalids()) {
                    // Hide error containers on last error
                    this.toHide = this.toHide.add(this.containers);
                }
                this.showErrors();
                return result;
            },
            checkSpecificRule: function(element, rule) {
                element = this.clean(element);
    
                // if radio/checkbox, validate first element in group instead
                if (this.checkable(element)) {
                    element = this.findByName(element.name).not(this.settings.ignore)[0];
                }
    
                var findRule = { },
                    checkRule = $(element).rules()[ rule ];
                var rules;
    
                if (checkRule) {
                    findRule[rule] = checkRule;
                    rules = findRule;
                }
    
                if (!rules) {
                    return;                
                }
                var dependencyMismatch = false;
                for (var method in rules) {
                    var rule = { method: method, parameters: rules[method] };
                    try {
                        var result = $.validator.methods[method].call(this, element.value.replace( /\r/g , ""), element, rule.parameters);
    
                        // if a method indicates that the field is optional and therefore valid,
                        // don't mark it as valid when there are no other rules
                        if (result == "dependency-mismatch") {
                            dependencyMismatch = true;
                            continue;
                        }
                        dependencyMismatch = false;
    
                        if (result == "pending") {
                            this.toHide = this.toHide.not(this.errorsFor(element));
                            return;
                        }
    
                        if (!result) {
                            this.formatAndAdd(element, rule);
                            return false;
                        }
                    } catch(e) {
                        this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
                            + ", check the '" + rule.method + "' method", e);
                        throw e;
                    }
                }
                if (dependencyMismatch)
                    return;
                if (this.objectLength(rules))
                    this.successList.push(element);
                return true;
            }
        });
    
    // Then use it like this...
    $('form').data('validator').elementWithRule('#Key', 'required');
    

    There didn’t appear to be any built in way to do this, so I just hacked something together! 🙂

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

Sidebar

Related Questions

Is there any way to trigger validation only in specific forms(controller's action), not globally
Is there any way to automatically create a trigger on creation of new table
I want to trigger one event on page load complete using javascript/jquery. Is there
Quick one; Netbeans 7.0 for PHP development: Is there any way to inform NetBeans
Is there any way to trigger update of updatepanel inside a web user control
There any way to let an application know if there is any specific change
Is there a way to determine if a trigger on any given table affects
Is there any way to trigger the focus() event on a <textarea> element and
Is there a way to determine if a specific Trigger in Quartz is in
Is there any way to trigger a reboot of the iPhone programmatically from within

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.