0

folks. I need some help. I'm trying to put the validation error message right next to my input file tag, but it gives me error in console. It's showing me a message, "TypeError: error.appendTo is not a function". This is a little piece of my code

$("#foto").rules("add", {
    required: true,
    extension: "jpeg|jpg|png",
    messages: {
        required: "Tolong Diisi",
        extension: "Harap Memilih Format yang Benar"
    },
    errorPlacement: function(error, element) 
    {
        error.appendTo($('#foto'));
    }
});

By the way, i'm using 'id' instead of 'name' which I've instantiated with

$("#formTambah").validate();
1
  • you can't place errorPlacement in the .rules(“add”) method, set it in validation. Commented Aug 25, 2017 at 14:00

1 Answer 1

1

As written in the comment above: you can't place errorPlacement in the .rules(“add”) method, set it in validation:

$("#foto").rules("add", {
    required: true,
    extension: "jpeg|jpg|png",
    messages: {
        required: "Tolong Diisi",
        extension: "Harap Memilih Format yang Benar"
    }
});

$("#formTambah").validate({
    errorPlacement: function(error, element) {
        if(element.attr("id") === "foto"){
            error.appendTo($('#foto'));
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

ok. gonna give a shot UPDATE wow, it works now. but it's not showing next to the element. Anyway, Thank you so much bro
@mending3 I have updated my answer - be aware that errorPlacement is a general Option for validation and you have to specify your "placement" behavior inside this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.