最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery Validator- Rule specific errorPlacement - Stack Overflow

programmeradmin1浏览0评论

I'm using jquery validator.

I've got a custom rule applied to a group of elements in my form. These inputs may also have other (standard) rules associated with them. I need to place the error message in one place if the element fails my custom rule, but another place if the element fails a standard rule.

I need something like -

errorPlacement: function(error, element) {
    if (error == MyCustomError) {
        // put my error at the top of the form
    } else {
        // put my error next to the element
    }

I can't see what that 'error' object really is (tried drilling down with firebug but it wasn't very informative).

The highlight option is even more difficult because I don't even have the 'error' parameter to use.

I'm using jquery validator.

I've got a custom rule applied to a group of elements in my form. These inputs may also have other (standard) rules associated with them. I need to place the error message in one place if the element fails my custom rule, but another place if the element fails a standard rule.

I need something like -

errorPlacement: function(error, element) {
    if (error == MyCustomError) {
        // put my error at the top of the form
    } else {
        // put my error next to the element
    }

I can't see what that 'error' object really is (tried drilling down with firebug but it wasn't very informative).

The highlight option is even more difficult because I don't even have the 'error' parameter to use.

Share Improve this question edited Feb 22, 2013 at 10:04 Aidan Ewen asked Apr 26, 2012 at 14:01 Aidan EwenAidan Ewen 13.3k11 gold badges65 silver badges91 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

error is the whole error label HTML element that is being added by jQuery Validate, i.e.:

<label class="error">Error message here</label>

So if you want to place the message differently if the message is "Do not do this", you can:

errorPlacement: function(error, element) {
if (error.text() == "Do not do this") {
    error.insertBefore("SELECT FIRST ELEMENT IN THE FORM HERE USING STANDARD JQUERY SELECTORS");
} else {
    error.insertAfter(element);
}
发布评论

评论列表(0)

  1. 暂无评论