PDF Editing Issues

How to Create a JavaScript Random Number Generator Excluding Specific Digits in PDF

Phi Hung VoPhi Hung Vo Oct 28, 2026 869 views

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.

How to Create a JavaScript Random Number Generator Excluding Specific Digits in PDF
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.
Before you start

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.

Solution 1Recommended

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.

1
Open JavaScript Editor

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.

2
Retrieve User Input

Get the user's input digit by defining the variable: var digit = this.getField("Exc1").valueAsString; (replace 'Exc1' with your actual input field name).

3
Handle Empty Input

Add an if statement to clear the output if the user hasn't entered a digit: if (digit == "") this.getField("StudentGroup").value = "";

4
Generate the Random Number

Write the else block to generate an initial random number between 0 and 999: var n = Math.floor(Math.random() * 1000);

5
Implement the Exclusion Loop

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);

6
Output the Valid Number

Finally, display the valid number in your target output field: this.getField("StudentGroup").value = n;

Use Regular Expressions to Exclude User-Defined Digits
Field Name Consistency: Make sure your field names in the script (like "Exc1" and "StudentGroup") exactly match the names of the text fields created in your PDF form.
Advanced PDF Editing with WPS

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. 1. Open Your PDF: Launch WPS Office and open your PDF document containing the form.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form controls.
  3. 3. Insert Text Fields: Select 'Text Field' to insert the input field for the excluded digit and the output field for the result.
  4. 4. Adjust Properties: Double-click the inserted fields to adjust their properties and ensure the field names match the ones referenced in your scripts.
Easily insert text fields, checkboxes, and buttons into your interactive PDF forms.Highly compatible with standard PDF formats and Adobe Acrobat forms.Lightweight software with a user-friendly, intuitive interface.Free to use for essential PDF reading, creating, and editing tasks.
microsoft office alternative - wps office

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.