0

I have the scenario like this:

On a page there are two text boxes (FromDate and ToDate) which shows JQuery DatePicker onkeydown, and below these text boxes I have a submit button.

Once the page is loaded, I dont want these date text boxes to be validated against rules, but I want to ensure that JQuery Validator works for me on button click.

I have to fire validation at page load to ensure other rules are fired, like many other controls existing on page are validated once page is loaded.

One way I thought to add Rules on click of button, but seems Validator load them and hook them much earlier, so even if the same object is updated (with additional rules), it doesnt work.

Please help how it can be done? Thanks in advance.

1
  • One idea is to change the ids of the text boxes when you click the button to match the rules of the validator. On page load they're not checked as the ids don't match the rules, then on 'click' you select the textboxes (e.g. by class) and replace with matching ids (e.g. removing "-temp" from the end of the id or something similiar). Commented Oct 26, 2012 at 11:00

1 Answer 1

1

To handle this scenario, I removed the rules which were event based, from Page Load rules. I dynamically added/removed the rules. Code sample below:

var Date1= {
        required: true,
        dateLesser: 'Date2',
        messages: {
            required: 'What ever',
            dateLesserThan: 'Date Should be less than'
        }
    };

addDateValidationRules: function (d) {
    if (d== 2) {
        $('#Date1').rules("add", Date1);
        $('#Date2').rules("add", Date2);
    }
    else {
        $('#Date1').rules("remove", "required");
        $('#Date1').rules("remove", "dateLesser");
        $('#Date2').rules("remove", "required");
        $('#Date2').rules("remove", "dateGreater");
    }
}

Hope it helps somebody.

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.