Jquery Validation: Call Valid without displaying errors?

故事扮演 提交于 2019-12-29 20:38:55

问题


Using JQuery Validation plugin, I am trying to call the .valid() method without the side-effects of displaying error messages on screen. I have tried a number of scenarios with no success, my latest being:

$('#signup').validate({
            showErrors: function() { return; },
            errorElement: "",
            errorClass: "",
            errorPlacement: function() { },
            invalidHandler: function() { },
            highlight: function(element, errorClass) { },
            unhighlight: function(element, errorClass) { }
        });

$('#displayPurposes').text("Valid: " + $("#EMailAddress_EmailAddress").valid());

Whilst the .valid() call is working correctly it is still causing side-effects of displaying error messages.

I don't want this to happen, help please.


回答1:


Well instead of trying to disable any error placement made by the plugin you could use CSS to 'force' hiding the create error messages elements. Something like:

label.myvalidationclass { display: none !important; }

Where myvalidationclass is either the default error class name or your own.

I'm aware it is not a very nice solution but in my opinion, displaying error messages is one of the core feature of the plugin.

d.




回答2:


You can do this without using CSS hacks by calling checkForm().

var isValid = $('#signup').validate().checkForm(); // true|false

checkForm() does not trigger UI changes.

However, there is one side effect. Form fields will now be validated on change/focus/blur, which may be undesirable. You can disable this behavior by resetting the submitted object:

$('#signup').validate().submitted = {};

Which results in:

var validate = $('#signup').validate(); // Get validate instance
var isValid = validate.checkForm(); // Valid?
validate.submitted = {}; // Reset immediate form field checking mode


来源:https://stackoverflow.com/questions/3922628/jquery-validation-call-valid-without-displaying-errors

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