jquery - how to use errorPlacement for a specific element?
Asked Answered
A

2

18

I have checkbox and a text follows it, like;
[checkbox] I agree

If the checkbox is not clicked when submitting, the current way of showing the error msg I have is(I use errorElement:"div");

[checkbox]
This field is required.
I agree**

I would rather like;
[checkbox] I agree
This field is required

Any idea how to get this done?.

The html wrap for the concerned checkbox and text elements I have is like this;
[div] [checkbox] I agree [/div] [div][/div]

I tried errorPlacment: as follows hoping to apply it just for that element alone;

...            
messages: {
    usage_terms: {
        required: "Must agree to Terms of Use.",
    //errorElement: "div"
    errorPlacement: function(error, element) {
        error.appendTo( element.parent("div").next("div") );
        }
    }
}
...


It didn't work. Any idea?.

Amritsar answered 27/10, 2010 at 11:52 Comment(0)
Y
40

errorPlacement isn't underneath messages (as an option), it's a global option, so instead it should look like this:

messages: {
  usage_terms: "Must agree to Terms of Use."
}
errorPlacement: function(error, element) {
  if(element.attr("name") == "usage_terms") {
    error.appendTo( element.parent("div").next("div") );
  } else {
    error.insertAfter(element);
  }
}

Here's we're just checking the element name when deciding where it goes, you could do another check though, for example giving all the ones that behave like this a class, and doing element.hasClass("placeAfter").

Yerkovich answered 27/10, 2010 at 12:3 Comment(0)
L
0

file html

<label for="Q008" class="control-label titular">Question text 008 by example</small></label>
<input type="text" class="form-control" name="Q008" id="Q008" maxlength="500" />
<div id="Q008error"></div>

<label class="control-label titular">Question text 009 by example</label>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_1" value="1" />
    <label for="Q009_1">Text 91</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_2" value="2" />
    <label for="Q009_2">Text 92</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_3" value="3" />
    <label for="Q009_3">Text 93</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_4" value="4" />
    <label for="Q009_4">Text 94</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_5" value="5" />
    <label for="Q009_5">Text 95</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_6" value="6" />
    <label for="Q009_6">Text 96</label>
</div>
<div id="Q009error"></div>

jquery validate file

errorPlacement: function(error, element) {
    var elementForm = "", containerError = "";
    offset = element.offset();
    elementForm = element.attr("name");
    containerError = "#" + elementForm + "error";
    error.prependTo(containerError);
    error.addClass('message');
}
Larianna answered 10/1, 2018 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.