21

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.

2 Answers 2

73

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
Sign up to request clarification or add additional context in comments.

5 Comments

+1: This is a far nicer solution, and allows the flexibility to only validate without messages when required.
This is solution I used and I can recommend to everyone.
Is there a way to find out if a certain input is valid without highlighting it?
@simaglei you should create a new SO question for that
This solution kind of messes up the generated HTML so that it no longer shows up in the same way. Could be an issue for CSS rules.
-6

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.

1 Comment

Apologies for the very late acceptance, but if I recall I did end up with something similar to hacking the css.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.