jQuery Validation - Hide error message and don't submit the form

▼魔方 西西 提交于 2019-12-25 03:52:23

问题


I am using jQuery Validate for a form.

I have a field that is required, but I don't want to display any error message for the field. I just want the form submission to be blocked.

I have tried setting the element error message to this:

messages: {
    required: "" 
}

However, due to default validation format that I don't have access to (it is a common file that I cannot change), I still see a background/box with a blank error message.

Is there a way to hide the rendering of the error message for a specific element, while still preventing the form submission?


回答1:


Quote OP:

"Is there a way to hide the rendering of the error message for a specific element, while still preventing the form submission?"

See the errorPlacement callback function, which controls where messages are placed.

Use a conditional so that you return false on the specific element, which effectively prevents a message from being placed, while allowing the default message placement on the rest.

$('#myform').validate({
    rules: {
        ....
    },
    errorPlacement: function(error, element) {
        if (element.attr('name') == 'myName') {
            return false;  // no message placement
        } else {
            error.insertAfter(element); // default message placement
        }
    }
});

DEMO: http://jsfiddle.net/3cndvv3o/



来源:https://stackoverflow.com/questions/26153650/jquery-validation-hide-error-message-and-dont-submit-the-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!