Make Field A 'required' if Field B equals X

By: Chris Adams | Asked: 06/11/2021
ForumsCategory: Code SnippetsMake Field A 'required' if Field B equals X
Chris AdamsChris Adams asked 3 years ago
This question was asked in the Formidable forms Slack group earlier today and the answer, although simple, is really useful and versatile. The code:
<script type="text/javascript">
jQuery(document).ready(function($){
$("#field_bttvs").attr("required", false);

$('select[name="item_meta[1601]"]').change(function(){

var isreq;
var val1 = $("select[name='item_meta[1601]']").val();

if (val1 == 'Yes')
{isreq = true;}
else if (val1 == 'No')
{isreq = false;}
else if (val1 == '')
{isreq = false;}

if(isreq){
$("#field_bttvs").attr("required", true);
}else{
$("#field_bttvs").attr("required", false);
}});
});
</script>
The snippet above uses a dropdown field and a text field but can be adjusted to use almost any fields. Replace #field_bttvs with the field key for your text field and 1601 with the field ID of your dropdown field.
1 Answers
Terry Tester answered 3 years ago
This is really useful. Thanks so much.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right