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