Skip to main content
Participant
October 29, 2024
Question

mandatory dropdown (but still submits if not changed)

  • October 29, 2024
  • 3 replies
  • 462 views

can someone help with I have a dropdown box names "Dropdown6" with the fields

  • Please Select
  • GBP
  • EUR
  • USD

I have set the box to be a mandatory field, but it still submits the form via email (mailto:) button and does not pick up that field has not changed from "please select" to either GBP / EUR or USD and shouldnt be submitted

 

 

This topic has been closed for replies.

3 replies

PDF Automation Station
Community Expert
Community Expert
October 29, 2024

It seems that there's a bug for the dropdown fields.  I like to use my own script that is place as a mouse up action in a button field.  Instead of setting fields as required, you list the field names in an array at the beginning of the script.  It works through them one at a time and when once all fields pass, the submit script is executed (mailDoc is used in this example).  Make sure you use my method in my original answer to give the dropdown a null default value:

 

var oFlds=["Text1","Text2","Text3","Text4","Check Box5","Group6"];
var count=0;
for(var i=0;i<oFlds.length;i++)
{
if(this.getField(oFlds[i]).type=="checkbox" || this.getField(oFlds[i]).type=="radiobutton")
{
if(this.getField(oFlds[i]).value=="Off")
{
if(app.alert(oFlds[i]+" is mandatory.",1)==1);
{count++;this.getField(oFlds[i]).setFocus();break;}
}
}
else
{
if(!this.getField(oFlds[i]).value)
{
if(app.alert(oFlds[i]+" is mandatory.",1)==1)
{;count++;this.getField(oFlds[i]).setFocus();break;}
}

}
}

if(count==0)
{this.mailDoc({cTo:"emailaddress@domain.com",cSubject:"Here's your form."})}

 

 

 

PDF Automation Station
Community Expert
Community Expert
October 29, 2024

"Please Select" is a value, so when the form is submitted, the field is considered to be filled out.  If you want the field with no value you would have to set the default value as no value, which can only be done with a script.  Run the following script in the console:

this.getField("Dropdown6").setItems(["","GBP","EUR","USD"]);

If you really want it to say "Please Select" when no value has been selected, enter the following custom format script after doing the above:

if(!event.value)

{event.value="Please Select"}

try67
Community Expert
Community Expert
October 29, 2024

That's not correct. The value that's selected as the default value can be anything, and it will be treated the same. As long as that value is selected the field is considered not filled in, and will fail the validation if also set as required.

When you use a script the default value is the first value in the array, but when setting it up manually it's the value you last clicked on (the one that's highlighted) when closing the Properties dialog. This is not explicitly stated, unfortunately, so many times people select the wrong value as the default one when creating or editing the field.

PDF Automation Station
Community Expert
Community Expert
October 29, 2024

Thanks for the clarification.  I've tested both ways (nothing as default value, and something else as default value), with the field set as required.  Both pass when the dropdown is not changed from the default value and the form submits.  How should this be done?

S_S
Community Manager
Community Manager
October 29, 2024

Hi @Lucy36077171bard,

 

Hope you are doing well. Thanks for writing in! 

 

You can add JavaScript to validate the dropdown before the form is submitted. Here's a simple example:

 

<form id="myForm" action="mailto:youremail@example.com" method="post" enctype="text/plain" onsubmit="return validateDropdown();">
    <select id="Dropdown6" name="currency">
        <option value="">Please Select</option>
        <option value="GBP">GBP</option>
        <option value="EUR">EUR</option>
        <option value="USD">USD</option>
    </select>
    <input type="submit" value="Submit">
</form>

<script>
function validateDropdown() {
    var dropdown = document.getElementById("Dropdown6");
    if (dropdown.value === "") {
        alert("Please select a currency.");
        return false; // Prevent form submission
    }
    return true; // Allow form submission
}
</script>

 

Hope this helps.


-Souvik