JavaScript Actions
Add custom form logic with JavaScript.
Form creation requires a Pro plan or higher. Form filling is available on all plans.
JavaScript in Forms
Penvio supports JavaScript for:
- Field calculations
- Validation
- Dynamic behavior
- Custom actions
Event Types
Field Events
- On Focus: Field receives focus
- On Blur: Field loses focus
- On Change: Value changes (after entry)
- On Keystroke: Each key press
- Calculate: When calculations run
- Validate: When validating input
- Format: When formatting display
Document Events
- Document Open: When PDF opens
- Document Close: When PDF closes
- Document Save: Before saving
Page Events
- Page Open: When page is displayed
- Page Close: When leaving page
Adding JavaScript
To a Field
- Open field properties
- Go to Actions tab
- Select event type
- Choose Run JavaScript
- Enter your script
Document-Level
- Click Forms → Document Scripts
- Add functions available throughout the document
- Call from field scripts
Common Scripts
Show/Hide Fields
// Hide a field
this.getField("fieldName").display = display.hidden;
// Show a field
this.getField("fieldName").display = display.visible;Set Field Value
this.getField("targetField").value = "New Value";Get Field Value
var value = this.getField("sourceField").value;Validation
// In validate event
if (event.value.length < 5) {
app.alert("Please enter at least 5 characters");
event.rc = false; // Reject change
}Format on Keystroke
// Allow only numbers
if (event.willCommit) {
if (isNaN(event.value)) {
event.rc = false;
}
}Conditional Logic
// Show field based on checkbox
var isChecked = this.getField("checkbox").value === "Yes";
this.getField("extraInfo").display = isChecked ? display.visible : display.hidden;Field Object Properties
| Property | Description |
|---|---|
value | Current field value |
display | Visibility state |
readonly | Read-only state |
required | Required state |
fillColor | Background color |
textColor | Text color |
borderColor | Border color |
Utility Functions
Alert
app.alert("Your message here");Confirm
var result = app.alert({
cMsg: "Are you sure?",
nType: 2, // Yes/No buttons
cTitle: "Confirm"
});
if (result === 4) { // Yes clicked
// proceed
}Calculate Total
var total = 0;
for (var i = 1; i <= 5; i++) {
var val = this.getField("item" + i).value;
if (!isNaN(val)) total += Number(val);
}
event.value = total;Tips
- Test scripts thoroughly
- Use meaningful field names
- Handle errors gracefully
- Comment complex logic
- Keep scripts efficient
Next Steps
- Calculations - Create calculated fields
- Buttons - Add submit, reset, and action buttons
- Text Fields - Add text input fields
- Create Forms Tutorial - Step-by-step form creation walkthrough
Last updated on