Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Error Message in a separate div using jQuery validation

I am using jQuery validation and I am wanting to display the error message in a div <div class="alert alert-error" id="errorContainer"> The default of showing below the forms does not work with my form design.

The validation script is linked to the page I have the form on and it looks like:

    $(function validate() {

    var rules = {
                rules: {
                    full_name: {
                        minlength: 2,
                        maxlength: 50,
                        required: true
                    },
                    address: {
                        minlength: 5,
                        required: true
                    },
                    city: {
                        minlength: 2,
                        required: true
                    },
                    state: {
                        minlength: 2,
                        maxlength: 2,
                        required: true
                    },
                    zip: {
                        minlength:5, 
                        maxlength:5,
                        required: true
                    },
                    phone: {
                        required: true,
                        phoneUS: true
                    },
                    phone_other: {
                        required: true,
                        phoneUS: true
                    },
                    email: {
                        required: true,
                        email: true
                    },
                    date_to_start: {
                        required: true
                    },
                    ssn: {
                        required: true,
                        ssn: true,
                    },
                    salary: {
                        required: true
                    }

                }
            };

        var validationObj = $.extend (rules, Application.validationRules);

        $('#employment-application').validate(validationObj);

});

Now I did try changing $('#employment-application').validate(validationObj); to the code below as well as tried adding it to the bottom of the page I have the form on, both gave negative results. The script seems to add style="display:none;" to the errorContainer and does not load any errors anywhere.

(tried changing $('#employment-application').validate(validationObj); to the below)

$("#employment-application").validate({
        errorContainer: "#errorContainer", 
        errorLabelContainer: "#errorContainer", 
        errorElement: "li"
    })

So re-cap, I am wanting to use jQuery Form Validation and display the error messages received in a separate div because the default does not work with my form design.

like image 605
user2371301 Avatar asked Jun 10 '13 10:06

user2371301


People also ask

How can I show error message below the textbox in jQuery validation?

Java script ready(function() { $("#basic-form"). validate(); }); This is based on the assumption that you have already added the required JavaScript files. Adding those lines of JavaScript will make sure that your form is properly validated and shows all the error messages.

What jQuery Unobstructive validation is?

An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.

What is valid () in jQuery?

valid() from the jQuery Validation plugin: $("#form_id"). valid(); Checks whether the selected form is valid or whether all selected elements are valid.


2 Answers

You can use the errorPlacement option to pass a callback like this: http://jsfiddle.net/cMhQ7/

I've used a standard of the div id being the input element's name concatenated with a _validate suffix but you can change as you wish.

HTML

<form id="employment-application" method="post">
    <input name="full_name" type="text" />
    <div id="full_name_validate"></div>
    <input type="submit" />
</form>

Javascript

$(function validate() {

    var rules = {
        rules: {
            full_name: {
                minlength: 2,
                maxlength: 50,
                required: true
            },
        },
        errorPlacement: function (error, element) {
            var name = $(element).attr("name");
            error.appendTo($("#" + name + "_validate"));
        },
    };

    $('#employment-application').validate(rules);

});
like image 187
Matt Harrison Avatar answered Sep 19 '22 14:09

Matt Harrison


If only want to change the default placement to certain elements, you can use this improvement. This implements the default behaviour of validate script

    errorPlacement: function(error, element){
        var name    = $(element).attr("name");
        var $obj    = $("#" + name + "_validate");
        if($obj.length){
            error.appendTo($obj);
        }
        else {
            error.insertAfter(element);
        }
    },
like image 43
Antonio AB Avatar answered Sep 18 '22 14:09

Antonio AB