JavaScript
Article

jQuery Custom Error Message

By Sam Deering

Quick code snippet posting for my reference. It shows a validation error message (which could be inside a container with a label and input or outside a container directly on an element, it works with both). If non are found it simply adds the msg after the input.

Also see: Solutions to Common jQuery Errors

//used to override the default message for custom validation
validationErrorMsg: function(elem, msg)
{
    if (elem.find('label.error').length > 0)
    {
        //found inside input group element
        elem.find('label.error').html(msg);
    }
    else if (elem.parent().find('label.error').length > 0)
    {
        //found inside parent (container for input group)
        elem.parent().find('label.error').html(msg);
    }
    else
    {
        //no error label found so put it after the input
        elem.after(''+msg+'');
    }
},

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.