I have a form that lists three phone fields (home, cell, work). I want the user to fill out at least one phone type field before being able to submit the form. I can't mark any required because some people don't have a home phone, others might not have a cell phone and so on. Is there a way to do this with Java?
Question
Question
Required to fill out one of three fields before submitting
Answer
The script missed the last line, please use following one:
$(document).ready(function(){ $('.Submit').prop('disabled', true); $('#q11, #q12, #q13').on('change', function() { var count = 0; $('.phone').each(function(i, obj) { if($(obj).find('input').val() == '') count ++; }); if(count == 3) $('.Submit').prop('disabled', true); else $('.Submit').prop('disabled', false); }); });
Replies
Are you able to have a radio button asking what type of phone number they have and then user a field rule to show the one selected and have it marked as required?
Hi Vanessa,
You can definitely accomplish that by using JavaScript.
For example, you can try something like below.
Here q1, q2, q3 are the id's for the phone, cell, home fields and I am disabling the submit button unless the user fills in at least one of the three fields.
$(document).ready(function(){ $('.Submit').prop('disabled', true); $('#q1, #q2, #q3').on('change', function() { var count = 0; $('.phone').each(function(i, obj) { if($(obj).find('input').val() == '') count ++; }); if(count == 3) $('.Submit').prop('disabled', true); else $('.Submit').prop('disabled', false); });
Hope this helps!
Kentaro,
I copied the JavaScript you provided and changed the id's to the correct numbers and it doesn't work. I am sorry I don't know much about JavaScript. Is there another element/selector I need to change the name on?
The script missed the last line, please use following one:
$(document).ready(function(){ $('.Submit').prop('disabled', true); $('#q11, #q12, #q13').on('change', function() { var count = 0; $('.phone').each(function(i, obj) { if($(obj).find('input').val() == '') count ++; }); if(count == 3) $('.Submit').prop('disabled', true); else $('.Submit').prop('disabled', false); }); });
Thank you Xiuhong Xiang! It worked perfect
Hi Vanessa,
Here is a code that might be a better work-around than simply disabling the button.
With this approach, you are initially making all of the three fields required so that when someone tries to submit the form without entering any of the three fields, it will display an error message. Although this solution only works with Forms 10.2, the power of this solution is that you can customize the error message (a new feature with Forms 10.2) to say something like "At least one of the three phone fields requires input". If people enter an input into one (or more) of the fields, the error message won't show up. Anyways, here is the code:
$(document).ready(function(){ var verify = function (t) { var val = $(t).val(); if (val != '') { removeRequiredFromAllFields(); } else if (val == '') { checkIfAllInputsAreEmpty(); } } var checkIfAllInputsAreEmpty = function () { var filledElement = $('.phone input').filter(function () { return this.value != ''; }); if(filledElement.length == 0) { addRequiredToAllFields(); } } var addRequiredToAllFields = function () { $('.phone').each(function (i, target) { $(target).find('.cf-required').css('display', ''); $(target).find('input').prop('required', true); }); } var removeRequiredFromAllFields = function () { $('.phone').each(function (i, target) { if($(target).find('.cf-required').css('display') != 'none') { $(target).find('.cf-required').css('display', 'none'); $(target).find('input').prop('required', false); $(target).find('div ul').remove(); $(target).find('input').removeClass('parsley-error'); } }); } $('#q1 input').on('change', function() { verify(this); }); $('#q2 input').on('change', function() { verify(this); }); $('#q3 input').on('change', function() { verify(this); }); });
Note: if you want this code to work with Forms 10.1, simply replace $(target).find('div ul').remove(); $(target).find('input').removeClass('parsley-error'); with $(target).find('div div').remove(); $(target).find('input').removeClass('user-error');
The downside is, it will take more work to customize the error message with Forms 10.1
Cheers,
Thanks Kentaro! I will have to try that once we upgrade to Forms 10.2.
This use case has just come up. I am very excited to use the long code shared here to turn off required fields once at least one field is filled. it works well to make the user enter a value. I can see that the red asterisk is removed from all three fields after one field is satisfied. However, I am getting a value required error page after submission. Is this back end validation or something new for 10.4?
This client is on 10.4.1.164.
Video demonstrating current behavior with script as is: https://www.screencast.com/t/rNtbLIdu
Turning backend validation off does resolve the issue, but what else does that impact?
The backend validation is to check the values after submit to make sure the values meet the requirements(such as required, out of range, incorrect format), this can prevent invalid submit requests send using advanced tools/scripts which bypass the front end validation. In your case as you don't want the values to follow the settings configured during design, you can turn off the backend validation, another option is you still enable backend validation and configure those fields as not required and use JavaScript to achieve the require logic.