2

Im using the bog standard form validation plugin. I have two radio buttons 'yes' 'no' and what im trying to do is set validation if a radio 'yes' is checked... Which works, but i need to then turn off validation if the 'no' radio is selected. Im trying the following...

<script type="text/javascript">
    $(document).ready(function()
    {
        $('.cbyes').click(function() 
        {
            $('.hiddenDiv').show('slow');
            $("#regForm").validate();
        });
        $('.cbno').click(function() {
            $('.hiddenDiv').hide('slow');
            $('#regForm')[0].reset();
        });
    });
</script>
skaffman
  • 398,947
  • 96
  • 818
  • 769
Dooie
  • 1,649
  • 7
  • 30
  • 47
  • Old question, but for whoever is still looking, have a look here: [jQuery validate - set conditional rules based on user selection](https://stackoverflow.com/questions/19543354/jquery-validate-set-conditional-rules-based-on-user-selection) – lehel Oct 17 '17 at 11:47

1 Answers1

10

Assuming there's no practical way to "turn off" validation once it's been applied to a form, can you use the validate method, but set it ignore everything in the form?

$(document).ready(function()
{
    $('.cbyes').click(function() 
    {
        $('.hiddenDiv').show('slow');
        $("#regForm").validate();
    });
    $('.cbno').click(function() {
        $('.hiddenDiv').hide('slow');
        $('#regForm')[0].reset();
        $('#regForm').validate({
            ignore: "#regForm *"
        });
    });
});

This example uses a dumb universal selector that should be replaced with something more appropriate for your particular form.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
twernt
  • 20,271
  • 5
  • 32
  • 41
  • 2
    If you want to submit the form and skip validation, the plugin's author has the answer: http://stackoverflow.com/questions/363268/how-do-remove-jquery-validation-from-a-form/1055371#1055371 – twernt Dec 30 '09 at 15:46
  • Note to the reader: This answer does not work with the latest version of the plugin. http://jsfiddle.net/tcZT4/ – Sparky Feb 07 '13 at 18:45
  • 1
    thank you most most kindly for the solution. It's just saved my day. – Matthew Setter Dec 19 '13 at 09:40