0

I can't disable form's validation. I have tried many solution from other questions and forums like:

1. $("#PatientForm").validate({ ignore: "*" });
2. $("#submitButton").addClass("cancel");
3. $('#PatientForm').validate().settings.ignore = ["*"];
4. $("#PatientForm").validate({ onsubmit: false });
5. $('#PatientForm').validate({ submitHandler: null });

And jQuery Validate still wants to validate all the fields. I am trying to diable on checkbox change. Checkbox change handler is throttled because I've check it with FireBug and all the calls (1. to 5.) executes without any error messages.

Here is my configuration of validation:

    // JQUERY VALIDATE - TURN ON VALIDATION ON SUBMIT + VALIDATE PESEL BEFORE SAVE
    $('#PatientForm').validate({
        submitHandler: function(form) {
            var pesel = $('#Pesel').val();
            var isValid = $(form).valid();
            $.ajax({
                type: 'GET',
                url: '@Url.Action("IsPeselExisting", "Admin")',
                data: { peselPar: pesel },
                success: function (msg) {
                    if (msg["exists"] == 'true')
                    {
                        if (isValid == true)
                        {
                            if (confirm("Wprowadzony pesel już istnieje! Czy na pewno chcesz dodać użytkownika o kolidującym peselu"))
                            {
                                form.submit();
                            }
                        }
                    }
                    else
                    {
                        if (isValid == true)
                        {
                            form.submit();
                        }
                    }
                }
            });
        }
    });

    // JQUERY VALIDATE - GENERAL SETTINGS
    $('#PatientForm').validate().settings.ignore = [];
    $("#PatientForm").validate({ ignore: ".ignored" });

    // JQUERY VALIDATE - GENERAL SETTINGS
    $.validator.addMethod("regex", function (value, element, regexp) {
        return regexp.test(value);
    }, "");

Thanks in advance...

4
  • Some possibly helpful hints here: stackoverflow.com/questions/363268/… Commented Jan 30, 2015 at 10:36
  • Another solution from mentioned question helped me. Disabling the form: jQuery('#form').validate().currentForm = ''; Enabling the form again: jQuery('#form').validate().currentForm = jQuery('#form')[0]; Why jQuery Validation is so unpredictable? :( Commented Jan 30, 2015 at 11:07
  • Still displays messages for validation (required, my custom regex) but allow to submit... what's wrong with this Validate library that it's so unpredictable? Commented Jan 30, 2015 at 12:09
  • There is nothing wrong with it and it's very predictable when used properly. Commented Jan 30, 2015 at 16:32

1 Answer 1

2

Once the jQuery Validate plugin is initialized on the form, there is no known method to disable it... period.

The reason your attempts have failed is because you cannot call the .validate() method more than once on the same form. Once initialized, any subsequent call to .validate() will be ignored.

$('#myform').validate({ /* your settings */ });  // <- initialize plugin

// later ...

$('#myform').validate({ /* new settings */ }); // <- ALWAYS IGNORED!!

However, to submit the form data without validation, put a class="cancel" on the type="submit" input or button element. The benefit of this is that although the validation is bypassed, the plugin is not, so if you're using the submitHandler, it will still be fired.

<input type="submit" class="cancel" value="SAVE" />

OR

<button type="submit" class="cancel">SAVE</button>

Working DEMO: http://jsfiddle.net/yan98120/


Other problems:

  1. You cannot do a form.submit() anywhere within the ajax() function; putting submit() within the ajax() success callback is just nonsense. The success callback within .ajax() means that the form was already successfully submitted with Ajax. Therefore, .submit() within success makes no sense at all when Ajax is already done sending the form.

  2. Your "settings" section also makes no sense. First you set ignore to [] using .settings.ignore and immediately after that, you set ignore to ".ignored" within the .validate() method. Pick one method and set of parameters for your settings and stick with it.

As I tried to explain on one of your other questions, please see the SO Tag Wiki page for a basic demo and hints on proper usage.

Sign up to request clarification or add additional context in comments.

7 Comments

So is there a way to leave 'regex' validation on some fileds and turn off 'required' on all fields?
I am using "settings" section that you mentioned first to diable hidden fileds validation and then enable '.ignored' validation.
@XaweryWiśniowiecki, you can dynamically change rules using the .rules('add') and .rules('remove') methods. See: stackoverflow.com/a/15836507/594235
I think you are not right in point 1. (other problems). This .ajax() is called just to check if IsPeselExisting (Pesel in the number that is different for anyone in Poland. I cannot do it with validation, but only to ask DB) and then if success (Pesel is different from any Pesels in DB) it is submitting the form. I think the only statement that is wrong there is var isValid = $(form).valid();. Because getting into submitHandler means that form is already valid. Am I right now?
@XaweryWiśniowiecki, yes, getting into submitHandler means the form is already valid. The exception to that rule is when you use class="cancel" on the submit button, then you get into submitHandler without validation. HOWEVER, if you just need to check the database to see if something already exists, you should use the remote rule/method.
|

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.