Question

I have the following inputs within a form:

<input type="text" id="Value1" />
<input type="text" id="Value2" />
<input type="text" id="Value3" data-val="true" data-val-match="not the same" data-val-match-other="Value1" />

And the following jQuery:

jQuery.validator.addMethod("match",
    function (value, element, params) {
        var other = $("#" + params);
        return value == other.val();
    });

jQuery.validator.unobtrusive.adapters.add
    ("match", ["other"], function (options) {
        options.rules["match"] = options.params.other;
        options.messages["match"] = options.message;
    });

When I change the value in Value3, it validates as expected. It reaches the breakpoint in the validator and the control appears as invalid if it does not match the Value in Value1. This is all as expected. But it is also doing this for Value2. Why is it doing this if I do not have the data-val attributes on the Value2 control?

Was it helpful?

Solution

I had not included the 'name attribute:

<input type="text" id="Value1" name="Value1" />
<input type="text" id="Value2" name="Value2" />
<input type="text" id="Value3" name="Value3" data-val="true" data-val-match="not the same" data-val-match-other="Value1" />

Once this was added it worked as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top