0

I can't seem to get jQuery validation to use the data-val-lookup attribute held against the input field it is validating:

Here is the code I am using to add the validation class rule.

$(function () {
    $.validator.addMethod('lookup', function (value, element, params) {
        var hiddenId = $(element).attr("id");
        var textBoxId = hiddenId + "_LookupText";
        return !(($('#' + hiddenId).val() == "") && ($('#' + textBoxId).val() != ""));
    });

    $.validator.addClassRules('lookup', { lookup: true });
});

Here is what I have for my lookup input fields:

<input class="lookup" data-val="true" data-val-lookup="Campaign must be a valid lookup item" data-val-number="The field Campaign must be a number." id="CampaignId" name="CampaignId" type="text" value="" />

<input type="text" id="CampaignId_LookupText" class="form-control" name="CampaignId_LookupText" />

The validation works but I get "Warning: No message defined for CampaignId" instead of the message.

I can specify a default message... but it needs to be field specific (containing the field name).

If I add a "title" attribute to the field, it is using this as the error message... but I want it to use data-val-myvalidation instead as this seems to be convention.

Thanks

1 Answer 1

2

It should be data-msg-<rule>

data-msg-lookup="Campaign must be a valid lookup item"

The customMessage method is as follows

customDataMessage: function (element, method) {
    return $(element).data("msg" + method[0].toUpperCase() + method.substring(1).toLowerCase()) || $(element).data("msg");
},
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. This fixed it! Why does the rest of the MVC populated validation use data-val-validationname?

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.