Bill Turner
Bill Turner

Reputation: 879

jquery dynamic validation rule ignoring message

Using the JQuery Validation plugin, I have composed the following, but find that the default validation message is displayed rather than my custom message (I have used the constant in other validations, so know that works!). What am i missing?

$("#pageform").validate();
$("input[id^=displayName]").each(function() {
    $(this).rules("add", {
        maxlength: AGHOSTMOBILEADMIN_VALIDATION.constants.displayNameMaxLength,
        messages: {
            maxLength: AGHOSTMOBILEADMIN_VALIDATION.format.displayName
        }
    });
});
$("input[id^=navigationLabel]").each(function() {
    $(this).rules("add", {
        maxlength: AGHOSTMOBILEADMIN_VALIDATION.constants.navigationLabelMaxLength,
        messages: {
            maxLength: AGHOSTMOBILEADMIN_VALIDATION.format.navigationLabel
        }
    });
});

Upvotes: 0

Views: 407

Answers (1)

rahul maindargi
rahul maindargi

Reputation: 5655

try this..

messages: {
            maxLength: AGHOSTMOBILEADMIN_VALIDATION.format.navigationLabel
        }

to

messages: {
            maxlength: AGHOSTMOBILEADMIN_VALIDATION.format.navigationLabel
        }

Note MaxLenth changed to maxlength (L become l)

So complete code should look like

$("#pageform").validate();
$("input[id^=displayName]").each(function() {
    $(this).rules("add", {
        maxlength: AGHOSTMOBILEADMIN_VALIDATION.constants.displayNameMaxLength,
        messages: {
            maxlength: AGHOSTMOBILEADMIN_VALIDATION.format.displayName
        }
    });
});
$("input[id^=navigationLabel]").each(function() {
    $(this).rules("add", {
        maxlength: AGHOSTMOBILEADMIN_VALIDATION.constants.navigationLabelMaxLength,
        messages: {
            maxlength: AGHOSTMOBILEADMIN_VALIDATION.format.navigationLabel
        }
    });
});

Here is working code

http://jsfiddle.net/cnrD3/

Upvotes: 1

Related Questions