How to Prevent an Email Domain Showing in an Empty PDF Form Field
Question details
The user needs a way to stop a fixed email domain from appearing in a rich-text span of a PDF form when the associated email username input field is left empty.
- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Designing a dynamic business card PDF form using Acrobat JavaScript and the event.richValue property.
- Observed behavior
- The fixed email domain is automatically appended and displayed on the document even if the employee leaves the email username input field completely blank.
Ensure you have access to the JavaScript editor in your PDF editing software and verify the exact field name for the email input (e.g., 'EmailAdd').
Use Conditional JavaScript to Hide the Domain
Modify your PDF JavaScript code to check if the email field contains a value before appending the domain string to the rich-text span.
When using JavaScript to dynamically populate rich-text fields in a PDF, static text like an email domain will always appear unless you explicitly tell the script to hide it when the source field is empty. Adding a simple ternary conditional operator solves this issue.
Open your PDF form in your PDF editor and enter the Form Editing mode to access the properties of your fields.
Right-click the target field displaying the rich text, select 'Properties', and navigate to the 'Calculate' or 'Format' tab where your custom JavaScript is located.
In your JavaScript code, retrieve the email field value as a string. For example: var email = this.getField('EmailAdd').valueAsString;
Set up a conditional check for the span text to ensure it only populates if the email variable has content. For example: spans[5] = new Object(); spans[5].text = email ? email + '@hsa.co.merced.ca.us' : '';
Assign the updated spans array to the event: event.richValue = spans; then click 'OK' or 'Close' to save the script.
Exit form editing mode and test the field. Type a username to ensure the domain appears, then delete the username to verify the domain completely disappears.
Create and Edit PDF Forms Easily with WPS Office
WPS Office features a powerful PDF editor that allows you to seamlessly manage, fill, and modify PDF forms. It ensures high compatibility with standard interactive PDF elements, making form handling highly efficient.
- 1. Open WPS PDF: Launch WPS Office and click on the 'PDF' tab on the main home screen.
- 2. Load Your PDF Form: Click 'Open' to browse your local files and select the PDF form you wish to edit.
- 3. Access Form Tools: Navigate to the 'Form' tab in the top ribbon to reveal tools for adding text fields, checkboxes, and buttons.
- 4. Edit Field Properties: Double-click any existing form field to modify its properties, appearance, and specific actions.
- 5. Save Your Changes: Click 'Save' or press Ctrl+S to preserve your updated interactive form features.

Frequently Asked Questions
Why does my PDF form field show 'null' or 'undefined' when empty?
This occurs in JavaScript when a field value isn't explicitly checked for an empty or null state before being concatenated with another string. Using a conditional operator (e.g., value ? value : '') prevents the script from printing 'undefined'.
Can I apply this conditional logic to other fields like phone number extensions?
Yes, you can use the exact same conditional JavaScript logic to append formatting, prefixes, or extensions only when the user has actually entered data into the target source field.
Where do I add custom JavaScript in a PDF form?
In most advanced PDF editors, you can right-click the specific form field, select Properties, and navigate to the 'Calculate', 'Format', or 'Actions' tabs to enter and edit your custom JavaScript code.
Does JavaScript formatting work on mobile PDF viewers?
JavaScript support varies heavily among mobile PDF apps. While conditional scripts work flawlessly in full desktop programs like Adobe Acrobat or WPS PDF, some mobile-only viewers may ignore formatting scripts.




