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

Question

Question

Make field required if checkbox is checked

asked on March 8, 2018

I would like to make a field required if a checkbox is checked.  I used the code in this thread: https://answers.laserfiche.com/questions/56221/dynamically-change-required-fields#67876

$(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');
        }
    })
});

But flipped it around with the intent to make it required if checked, instead of not required, here is a sample:

 

//This is for the Contact Resistance Test
$(document).ready(function () {
    $('.ckboxConResis input').change(function () {
        if ($(this).is(':checked')) {
            $('.reqNAConResis label').append('<span class="cf-required">*</span>');
            $('.reqNAConResis input').attr('required', 'True');
            //alert('if');

        } else {
            $('.reqNAConResis span.cf-required').remove();
            $('.reqNAConResis input').removeClass('required').removeAttr('required');  
        }
    })
});

But it doesn't work, it puts the little red * next the field if the checkbox is checked, but I can still submit the form.

Thanks in advance,

Alon

0 0

Answer

SELECTED ANSWER
replied on March 11, 2018

For multi-line field, you need to update the "input" with "textarea" as the html elements are different. The script would be:

//This is for the Contact Resistance Test
$(document).ready(function () {
    $('.ckboxConResis input').change(function () {
        if ($(this).is(':checked')) {
            $('.reqNAConResis label').append('<span class="cf-required">*</span>');
            $('.reqNAConResis textarea').attr('required', 'True');
            //alert('if');

        } else {
            $('.reqNAConResis span.cf-required').remove();
            $('.reqNAConResis textarea').removeClass('required').removeAttr('required');  
        }
    })
});

That would add the red * and make the field acting like a required field.

 

0 0

Replies

replied on March 8, 2018

Hi Alon,

The updated script works for me. What is the field type for the field with the "reqNAConResis" class? Is there default value on the fields? And which version of Forms do you use?

0 0
replied on March 9, 2018

Hi Rui,

Thanks for the help!  Just to clarify, for you the script adds the red * and makes it so you can't submit the form if that field is blank?

The field with the "reqNAConResis" is a multi-line field with no formula and no default value.  It is in a section that is collapsed by default.  I tried setting it to open by default, but that didn't help.

We are using Forms Pro 10.2.1.246

0 0
SELECTED ANSWER
replied on March 11, 2018

For multi-line field, you need to update the "input" with "textarea" as the html elements are different. The script would be:

//This is for the Contact Resistance Test
$(document).ready(function () {
    $('.ckboxConResis input').change(function () {
        if ($(this).is(':checked')) {
            $('.reqNAConResis label').append('<span class="cf-required">*</span>');
            $('.reqNAConResis textarea').attr('required', 'True');
            //alert('if');

        } else {
            $('.reqNAConResis span.cf-required').remove();
            $('.reqNAConResis textarea').removeClass('required').removeAttr('required');  
        }
    })
});

That would add the red * and make the field acting like a required field.

 

0 0
replied on March 12, 2018

Thanks Rui, I should have known that!

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

Sign in to reply to this post.