Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery turn off form validation

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>
like image 840
Dooie Avatar asked Nov 20 '25 09:11

Dooie


1 Answers

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.

like image 99
twernt Avatar answered Nov 22 '25 02:11

twernt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!