Localize jQuery Validate

May 20, 2015
Here is a code snippet on how to localize the custom error messages for the jQuery Validation Plugin, assuming I have already imported the proper libraries.

I have created a separate function in my script to set the proper error messages.

function addCustomMessages(){
    $("#inputName").rules("add", {
        messages: {
            required: errorRequired,
            minlength: errorMinLength
        }
    });
}

where errorRequired and errorMinLength are global variables that holds the localized messages.

The function will be called every time the language is changed.

$("#languageSelector").change(function(){
 // code here to get the translations
 addCustomMessages(); // code that will load the messages for the jQuery validation plugin
});

Works like magic. :)

I'll try to add a fiddle next time to further clarify.

Other helpful links:


No comments:

Post a Comment