jQuery override default validation error message display (Css) Popup/Tooltip like

前端 未结 7 1208
既然无缘
既然无缘 2020-11-28 17:16

I\'m trying to over ride the default error message label with a div instead of a label. I have looked at this post as well and get how to do it but my limitations with CSS a

相关标签:
7条回答
  • 2020-11-28 18:17

    You can use the errorPlacement option to override the error message display with little css. Because css on its own will not be enough to produce the effect you need.

    $(document).ready(function(){
        $("#myForm").validate({
            rules: {
                "elem.1": {
                    required: true,
                    digits: true
                },
                "elem.2": {
                    required: true
                }
            },
            errorElement: "div",
            wrapper: "div",  // a wrapper around the error message
            errorPlacement: function(error, element) {
                offset = element.offset();
                error.insertBefore(element)
                error.addClass('message');  // add a class to the wrapper
                error.css('position', 'absolute');
                error.css('left', offset.left + element.outerWidth());
                error.css('top', offset.top);
            }
    
        });
    });
    

    You can play with the left and top css attributes to show the error message on top, left, right or bottom of the element. For example to show the error on the top:

        errorPlacement: function(error, element) {
            element.before(error);
            offset = element.offset();
            error.css('left', offset.left);
            error.css('top', offset.top - element.outerHeight());
        }
    

    And so on. You can refer to jQuery documentation about css for more options.

    Here is the css I used. The result looks exactly like the one you want. With as little CSS as possible:

    div.message{
        background: transparent url(msg_arrow.gif) no-repeat scroll left center;
        padding-left: 7px;
    }
    
    div.error{
        background-color:#F3E6E6;
        border-color: #924949;
        border-style: solid solid solid none;
        border-width: 2px;
        padding: 5px;
    }
    

    And here is the background image you need:


    (source: scriptiny.com)

    If you want the error message to be displayed after a group of options or fields. Then group all those elements inside one container a 'div' or a 'fieldset'. Add a special class to all of them 'group' for example. And add the following to the begining of the errorPlacement function:

    errorPlacement: function(error, element) {
        if (element.hasClass('group')){
            element = element.parent();
        }
        ...// continue as previously explained
    

    If you only want to handle specific cases you can use attr instead:

    if (element.attr('type') == 'radio'){
        element = element.parent();
    }
    

    That should be enough for the error message to be displayed next to the parent element.

    You may need to change the width of the parent element to be less than 100%.


    I've tried your code and it is working perfectly fine for me. Here is a preview: alt text

    I just made a very small adjustment to the message padding to make it fit in the line:

    div.error {
        padding: 2px 5px;
    }
    

    You can change those numbers to increase/decrease the padding on top/bottom or left/right. You can also add a height and width to the error message. If you are still having issues, try to replace the span with a div

    <div class="group">
    <input type="radio" class="checkbox" value="P" id="radio_P" name="radio_group_name"/>
    <label for="radio_P">P</label>
    <input type="radio" class="checkbox" value="S" id="radio_S" name="radio_group_name"/>
    <label for="radio_S">S</label>
    </div>
    

    And then give the container a width (this is very important)

    div.group {
        width: 50px; /* or any other value */
    }
    

    About the blank page. As I said I tried your code and it is working for me. It might be something else in your code that is causing the issue.

    0 讨论(0)
提交回复
热议问题