3

I'm using the plugin of validation in jquery. There is a very strange thing is that, when I add rules in this way

$(document).ready(function () {
$.validator.addMethod("endDateGreaterThan", function (value, element, params) {
            if (value != "" && $(params).val() != "") {
                var endDate = formatValidateDate(value);
                var startDate = formatValidateDate($(params).val());
                if (startDate == null || endDate == null)
                    return true;
                return new Date(endDate) >= new Date(startDate);
            }
            return true;
        }, "");

var validator = $("#form").validate({
            rules: {
                StartDate: "required",
                EndDate: {
                    required: true,
                    endDateGreaterThan: "#StartDate"
                },                  
                DiscountRate: {
                    required: true,
                    max: 100,
                    min: 1
                }
            },
            messages: {
                StartDate: {
                    required: "ErrorMessageStartDateRequired"
                },
                EndDate: {
                    required: "ErrorMessageEndDateRequired",
                    endDateGreaterThan: "ErrorMessageEndDateGreaterThanStartDate"
                },
                DiscountRate: {
                    required: "ErrorMessageDiscountRateRequired",
                    max: "Global.ErrorMessageDiscountRateRange",
                    min: "Global.ErrorMessageDiscountRateRange"
                }
            }
      });

There is no rule added. If I debug the js file and step into the validate method in jquery.validation.js file, I found that the option passed to validate method is empty.

But If I add rules dynamically, just like

$("#StartDate").rules("add", {
            required: true,
            messages: {
                required: "StartDate is required"
            }
        });

The rule will be added just as what it should be.

I'm using ASP.net mvc.

Any suggestions are appreciate.

1
  • 1
    Show the rendered HTML of the form. In your first case, when rules are defined within .validate(), you must use the name attribute as the selector. In your second case, with the rules('add') method, you're targeting your field by its id attribute. See the difference? If we could see the HTML of the form, would your rules selectors match the fields' name attribute? Commented Jun 5, 2013 at 16:20

2 Answers 2

1

Chances are that your html is wrong. Every input that you reference in your validate code must have a name attribute. So this is wrong:

<input type="text" id="StartDate" />

This is right:

<input type="text" name="StartDate" id="StartDate" />

See the documentation:

The name attribute is '''required''' for input elements, the validation plugin doesn't work without it. Usually name and id attributes should have the same value.

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

2 Comments

This is true, however only a really good guess until the OP shows his rendered HTML.
True, @Sparky - seemed like the only possible answer to me, regardless of seeing the HTML... I agree the question can be improved!
0

Here is my example which is working fine:

    $("#formControl").validate({
        rules: {
            ElementTypeUid: "required",
            InternalIdentifier: "required"
        },
        messages: {
            ElementTypeUid: "Some error message",
            InternalIdentifier: "Some error message"
        }
    });

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.