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

Question

Question

Javascript to make field required based on value of another field.

asked on February 19, 2018 Show version history

I need to write javascript that says if Age is greater than 5 YR/ 0 MO, then make field Last Manufacture Recertification question required.  The Age field is calculated based off the manufacture date and current date.  I have tried have two fields (one required/ one not required) using field rules, but it doesn't work because the Last Manufacture Recertification is set to only appear when the equipment type is an SRD.  The field rules breakdown because two different rules act on the same field.  Can anyone help me write this if then statement in javascript?  Any help is greatly appreciated.

0 0

Replies

replied on February 19, 2018

In the Javascript section you can do something like this. Just give the field that you want required/no-required the class "chkReq" or adjust to your preference

It gets fancier when you want to remove the asterisk on the fly and put it back, depending on what the field contains.

$( document ).ready(function() {
    
  
  IF SOMETHING {
    
   $('.chkReq input').removeAttr('required');
    
  }
  ELSE
  {
   
   $('.chkReq input').attr('required', true);
    
  }
  
  
});

 

0 0
replied on May 5, 2018

Sure. For the multi-line field use:

$('.chkReq textarea')

instead of input. Just put it right below the code for the input. It would be something like:

$( document ).ready(function() {
    
  
  IF SOMETHING {
    
   $('.chkReq input').removeAttr('required');
   $('.chkReq textarea').removeAttr('required');
    
  }
  ELSE
  {
   
   $('.chkReq input').attr('required', true);
   $('.chkReq textarea').attr('required', true);
    
  }
  
  
});

 

0 0
replied on May 7, 2018

Oh, of course.  Thank you so much!! laugh

0 0
replied on March 6, 2019 Show version history

Is there a way to add in the required icon when you add the attribute?

<span class="cf-required">*</span>

 

0 0
replied on November 15, 2024

you can add the required icon using the following:

//q number
$('#qxx label').append('<span class="cf-required">*</span>');

or
//classname
$('.classname label').append('<span class="cf-required">*</span>');

 

0 0
replied on May 4, 2018

Can this be applied to more than one field if I use the same "chkReq" class ?  It's working on the first field (a date field), but not the second (a multi-line field). 

 

Thanks! 

0 0
replied on January 6, 2023

Validate field as required or optional using field rules is supported in new form designer with Forms 11 Update 3.

You can see other changes of Forms 11 Update 3 from  https://support.laserfiche.com/kb/1014413/list-of-changes-for-laserfiche-forms-11-update-3 and get Forms 11 Update 3 from Laserfiche 11 package  https://support.laserfiche.com/kb/1014263/software-versions-and-fixes-included-in-the-laserfiche-11-download-package.

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

Sign in to reply to this post.