1

I am working on an in application solution for affords me limited page real estate and therefore cannot apply contained error messages for every required field. I therefore am trying to create a solution that would highlight the affected required fields and only create a custom message for specified fields like email address, telephone and password in my form.

I am presently working with the basic shell(which is working fine for contained messages)

    var v = $("form").validate({
        rules: {
            email: {
                required: true,
                email: true                 
            },
            confirmEmail: {
                required: true,
                email: true,
                equalTo: "#email"
            }
        },
        messages: {
            email: "Please enter a valid email address",
            confirmEmail: {
                required: "Please enter a valid verify email address",
                equalTo: "Both email addresses must be the same."
            }
        },
        errorContainer: errcontainer,
        errorLabelContainer: $("ol", errcontainer),
        wrapper: 'li',
        onkeyup: false,
        onblur: false
    });     

Thanks in advance for any help or advice.

2
  • What do you need help with? The highlighting of fields in errors? Suppressing unwanted error messages? Commented Jan 12, 2012 at 17:25
  • I really jus need to understand how to supress the numerous invalid value messages while still having a generalised message and also including custom messages for emails and any other designated fields. Commented Jan 12, 2012 at 19:54

1 Answer 1

1

What you want to do is implement the showErrors hook in the validate plugin.

You get passed a map of the error messages, and then an array of those same messages associated with the form element that produced them.

Your best bet is then to just take a copy of the default handler (called defaultShowErrors in the validate plugin) and modify it slightly (to only show labels for the ones you care about).

You'd then have something like this in your validate options:

showErrors: function(errorMap, errorList) {
    for (var i = 0; this.errorList[i]; i++) {
        var error = this.errorList[i];
        //this is the only real part you modify
        if (error.element.id == 'email' || error.element.id == 'confirmEmail') {
            this.showLabel(error.element, error.message);
        }
        this.settings.highlight && this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass);
    }
    //a whole bunch more stuff after this, from jquery.validate.js
}

See this idea in action here: http://jsfiddle.net/ryleyb/D3tfu/

Sign up to request clarification or add additional context in comments.

3 Comments

this works great, but my intention is to provide a summary message for the outstanding fields and provide custom messaging for the designated like email etc. Should I be doing this within the showErrors hook too? Ive tried a few options and am yet to get anything to work. Any Ideas?
one other thing I forgot to mention in my initial question was this form is accordianised and has an additional method to validate contained areas. This solution was provided in this post
Yeah, showErrors would be an appropriate place to generate your summary message too.

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.