2

i try to custom error message using errorPlacement but when error show up it keep repeat when i press body or Send Message button.

Below is my jQuery code and here is my jsfiddle

   $(document).ready(function () {
       $("#myform").validate({
           rules: {
               message: {
                   required: true,
                   minlength: 5,
                   maxlength: 753
               }
           },
           debug: true,
           errorPlacement: function (error, element) {
               error.appendTo('#invalid');
           },
           messages: {
               message: {
                   required: "Enter your text message",
                   minlength: jQuery.format("Enter at least {0} characters"),
               }
           }
       });
   });
rusly
  • 1,504
  • 6
  • 28
  • 62

3 Answers3

4

Use errorLabelContainer instead of errorPlacement if you want your errors inside a specific container instead of next to each input element.

Your jsFiddle modified: http://jsfiddle.net/mXm4N/

$(document).ready(function () {
    $("#myform").validate({
        rules: {
            message: {
                required: true,
                minlength: 5,
                maxlength: 753
            }
        },
        errorLabelContainer: '#invalid',
        messages: {
            message: {
                required: "Enter your text message",
                minlength: jQuery.format("Enter at least {0} characters"),
            }
        }
    });
});

HTML:

<div id="invalid"></div>
<form>
    ...
</form>
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • @souzan - what does that have to do with anything? Everyone has a unique situation and nobody has even seen your code. If you have an issue please post a new question rather than asking within a comment. – Sparky Sep 10 '19 at 14:09
1

The element with the id invalid must be contained within your form.

If you use errorPlacement as below the plugin only look within the form for the element, it cannot find it and will just insert another one.

errorPlacement: function (error, element) {
  // element with id=invalid must be within the form being validated,
  // otherwise use errorLabelContainer
  error.appendTo('#invalid');
}

If you are trying to acheive a list of errors on its own, rather than having them placed next to the fields then use errorLabelContainer.

politus
  • 5,996
  • 1
  • 33
  • 42
0

Have a look at this page:

http://docs.jquery.com/Plugins/Validation

and read the bit about recursion and the submit handler - that might help.

Stevo
  • 2,601
  • 3
  • 24
  • 32