How to Create a JavaScript Random Number Generator Excluding Specific Digits in PDF
Question details
The user needs to write a JavaScript function for an interactive PDF that generates a random number between 0 and 999, specifically omitting any number that contains a digit previously entered by the user.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Developing custom interactive PDF forms that require random number generation with dynamic, user-defined exclusion rules.
- Observed behavior
- Needs a working JavaScript script to retrieve a value from an input field, use it as an exclusion criteria in a regular expression, and loop the random number generation until a valid number is found.
Ensure your PDF editor supports JavaScript execution and that you have added the necessary text fields (e.g., one for the excluded digit input and one for the random number output) to your PDF form.
Use Regular Expressions to Exclude User-Defined Digits
Implement a JavaScript while loop that continuously checks a generated random number against the user's input using a RegExp.
This approach takes the value from a specific input field, creates a dynamic regular expression, and continuously generates a new random number from 0 to 999 until the generated number does not contain the excluded digit. It also handles empty inputs safely.
Open your PDF form in editing mode, right-click the target form field or button, select Properties, and navigate to the Actions tab to add a custom JavaScript.
Get the user's input digit by defining the variable: var digit = this.getField("Exc1").valueAsString; (replace 'Exc1' with your actual input field name).
Add an if statement to clear the output if the user hasn't entered a digit: if (digit == "") this.getField("StudentGroup").value = "";
Write the else block to generate an initial random number between 0 and 999: var n = Math.floor(Math.random() * 1000);
Add a while loop to test and regenerate the number if it contains the excluded digit: while (new RegExp(digit).test(String(n))) n = Math.floor(Math.random() * 1000);
Finally, display the valid number in your target output field: this.getField("StudentGroup").value = n;

Hardcode a Specific Digit for Exclusion
Use this simplified method if the excluded digit is fixed and does not require active user input.
Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides comprehensive PDF editing tools that allow you to insert, edit, and manage interactive form fields, making document processing faster and more efficient.
- 1. Open Your PDF: Launch WPS Office and open your PDF document containing the form.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form controls.
- 3. Insert Text Fields: Select 'Text Field' to insert the input field for the excluded digit and the output field for the result.
- 4. Adjust Properties: Double-click the inserted fields to adjust their properties and ensure the field names match the ones referenced in your scripts.

Frequently Asked Questions
Can I exclude multiple digits using this JavaScript method?
Yes, you can modify the regular expression to test for multiple characters. For example, using a character class like /[357]/ in your RegExp will exclude any numbers containing 3, 5, or 7.
Why does the script freeze or crash the PDF reader?
If your exclusion criteria are too broad (for example, excluding all digits 0-9), the while loop will become an infinite loop. Always ensure that a valid number can realistically be generated to break the loop.
How do I access the JavaScript console in a PDF editor?
In most advanced PDF editors, you can access the JavaScript console by pressing 'Ctrl + J' (Windows) or 'Cmd + J' (Mac), or by navigating to a form field's Properties and selecting the 'Actions' tab to add a custom script.
Do these JavaScript functions work in mobile PDF readers?
Support for JavaScript in PDF forms is generally limited on mobile devices. Interactive scripts typically require a full desktop PDF editor or viewer to function correctly.




