6

I was wondering how to display a single error message above the form instead of individual field messages. like this form has http://jquery.bassistance.de/validate/demo/marketo/step2.htm

I know it has something to do with handles but not exactly sure how or where to put them

    <script>
    $(document).ready(function(){
    $("#valform").validate();
    });
    </script>

this is the code i have that uses all default validation

1 Answer 1

3

You should use invalidHandler for this functionality. Something like this should do:

$("#myform").validate({
    invalidHandler: function(form, validator) {
        var errors = validator.numberOfInvalids();

        if (errors) {
            $("#error-message").show().text("You missed " + errors + " field(s)");
        } else {
            $("#error-message").hide();
        }
    }
});

Example: http://jsfiddle.net/KheRr/1/

If you want to hide the default error messages, you should specify "" as the error message for the field and validation type:

$("#myform").validate({
    invalidHandler: function(form, validator) {
        var errors = validator.numberOfInvalids();

        if (errors) {
            $("#error-message").show().text("You missed " + errors + " field(s)");
        } else {
            $("#error-message").hide();
        }
    },
    messages: {
        field1: {
            required: "" // You'll have to do this for each field and validation type.
        }
    }
});

Example: http://jsfiddle.net/KheRr/2/

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

1 Comment

thanks, one more thing, how do i hide the default error messages that show on each field?

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.