javascriptjqueryjquery-validateerrorplacement

Jquery validate if/else errorplacement and plus placement in div


I have a form where the placement for radiobuttons is different then for the other elements in the form.

I would like to keep this but also add the errors into a div. Is it possible to do this?

Right now i have this for the radiobutton placement

$('#newform').validate({
    onfocusout: function(element) { $(element).valid(); },
    errorPlacement: function(error, element) {
        if ( element.is(":radio") ) {
            error.prependTo( element.parent() );  
        }
        else { // This is the default behavior of the script  
            error.insertAfter( element );
        }  
    },
    rules{XXX},
    messages{}

And so on. How can I add the errormessages to a div without removing the current funcionality?


Solution

  • use the errorLabelContainer option :

    $("#myform").validate({
       errorLabelContainer: "#messageBox",
       wrapper: "li"
    })
    

    this will place all errors in a list (li) within a DOM element with an ID of messageBox

    See here

    Sorry i missunderstood your problem ....

    try this :

    $('#newform').validate({
            onfocusout: function(element) { $(element).valid(); },
            errorPlacement: function(error, element) {
                if ( element.is(":radio") ) {
                    error.prependTo( element.parent() );  
                }
                else { // This is the default behavior of the script  
                    error.insertAfter( element );
                }
                error.appendTo('#errordiv');
            },
    

    This will keep your current functionallity but also append the message to the div with an id of 'errordiv'