You are viewing limited content. For full access, please sign in.

Question

Question

How to change the required status based on a field value?

asked on October 1, 2018

I have reviewed other posts out there and have found this coding to add or remove a required status based on a checkbox.  Wondering how I would alter it to be based on a field value?  I have a field (q15) on my form that contains a formula that returns a TRUE or FALSE value based on other fields.

I want a certain field (q79) to be required if my formula comes back as TRUE and not required if it is false.

 

$(document).ready(function () {
    $('.checkbox input').change(function () {
        if ($(this).is(':checked')) {
            $('.req span.cf-required').remove();
            $('.req input').removeClass('required').removeAttr('required');

        } else {
            $('.req label').append('<span class="cf-required">*</span>');
            $('.req input').attr('required', 'True');
        }
    })
});

 

Thanks in advance for any help you can provide.

0 0

Replies

replied on October 1, 2018 Show version history
$(document).ready(function () {
    $('#q15 input').change(function () {
        if ($(this).val() == "FALSE") { // Are you testing a string value or literal boolean? right right of this comparison will change depending
            $('#q79 span.cf-required').remove();
            $('#q79 input').removeAttr('required'); // Assuming this field is an input; change to correct field type if otherwise

        } else {
            $('#q79 label').append('<span class="cf-required">*</span>');
            $('#q79 input').attr('required', 'True');
        }
    })
});

 

1 0
replied on October 1, 2018

Thank you.  This works great as long as the value does not change.  When they change the date to force the formula to read true, it puts ** after the field instead of removing the *.

A little more description is that they have to be filling out this form at least 20 days prior to the application deadline.  The calculation looks at the current date and the application deadline.

If I put in a date that is less than 20 days away, it works fine and adds the required *.  But when I go back and change the date to be more than 20 days out, it just adds a second * to the field.

doublestar.png
doublestar.png (5.04 KB)
0 0
replied on October 2, 2018

Hi Jennifer,

Try removing the first line in the else {} statement and see if that helps.

~Rob

0 0
replied on October 2, 2018

I ended up just swapping the if then portions and making it react to TRUE instead of false.

 

$(document).ready(function () {
    $('#q15 input').change(function () {
        if ($(this).val()=="TRUE") {
            $('#q79 label').append('<span class="cf-required">*</span>');
            $('#q79 input').attr('required', 'True');
        } else {
          
       $('#q79 span.cf-required').remove();
            $('#q79 input').removeClass('required').removeAttr('required');

        }
    })
});

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.