1

Im just trying to customize where I put the error messages generated by jQuery validation. I have the following code:

$('document').ready(function(){
    $('form').validate({
        rules: {
           a: {required:true, minlength:2}
        },      
        messages: {
           a: {required: "enter your name!"}       
        },
        errorPlacement: function(error, element) {
         if(element.attr('name' == 'a')){
             error.appendTo($('#restErrorDate'));                    
         }
        },
        debug:true
    });
    $('#a').blur(function(){
        $("form").validate().element("#a");
    });
});

here is the html:

<div>
<form action="#">
    <input type="text" name="a" id="a">
</form>
</div>
<div id="restErrorDate" class="restErrorDate" style="border:1px solid blue;"></div>

For some reason it is not working. Thanks so much in advance for your help...this little thing has been keeping me working for a long time. Here is the jsfiddle:

http://jsfiddle.net/mmw562/S7AKK/38/

1 Answer 1

5
    $('document').ready(function(){
    $('form').validate({
        rules: {
           a: {required:true, minlength:2}
        },      
        messages: {
           a: {required: "enter your name!"}       
        },
        errorPlacement: function(error, element) {
         if(element.attr('name') == 'a'){
            //               ^--------------missing brackets              
             error.appendTo($('#restErrorDate'));                    
         }
        },
        debug:true
    });
    $('#a').blur(function(){
        $("form").validate().element("#a");
    });
});​

DEMO

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

1 Comment

Good! And even better if you put div#restErrorDate inside the form element, e.g. right before the form end tag. That way the error message disappears when the input element gets valid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.