Avg. Rating 5.0

Problem

Setting a form field to “Required” in the “General Properties” tab forces the user to fill in the selected form field. But there are cases when you need the user to fill out a particular field before moving on to the rest of the form. You could use JavaScript to dynamically enable or disable parts of the form but that can be cumbersome to keep track of. The ideal situation would be to, where appropriate, force the user to enter data into a required field before moving to another field.

Solution

Add the following code to the “On Blur” action of the field you want to be required.

Detailed explanation

Add the following code to the "On Blur" action of the field you want to be required.

f = getField(event.target.name)
if (f.value.length == 0)
   {    
   f.setFocus()    
   //Optional Message - Comment out the next line to remove
   app.alert("This field is required. Please enter a value.")
   }

This code will be triggered when the user tries to either tab out of the field or uses the mouse to click into another field. It tests to see if the length of the contents of the field is 0 (an empty field) and then places the cursor back in the field if it is. If the length of the contents is greater than 0 (a field value has been entered) the script does nothing and the user can move on.

You can download a PDF file of this tip that includes example fields here.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes