문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top