---
title: "PDF form validations using JavaScript | Nutrient SDK"
canonical_url: "https://www.nutrient.io/guides/web/forms/javascript-validation/"
md_url: "https://www.nutrient.io/guides/web/forms/javascript-validation.md"
last_updated: "2026-05-15T09:08:03.696Z"
description: "Learn how to implement form validation in Nutrient Web SDK and Nutrient Electron SDK using JavaScript. Discover easy techniques to ensure correct form field values."
---

# Validate PDF forms using JavaScript

It’s possible to perform form validation through document-embedded JavaScript in Nutrient Web SDK and Nutrient Electron SDK.

Working with PDF JavaScript is available when using [Web SDK](/guides/web.md) in the browser. For more information, refer to the [operational mode](/guides/web/about/operational-modes.md) guide.

JavaScript support is enabled by default. To disable it, set [`NutrientViewer.Options.PDF_JAVASCRIPT`](https://www.nutrient.io/api/web/variables/NutrientViewer.Options.PDF_JAVASCRIPT.html) to `false` before loading Nutrient:

```js

NutrientViewer.Options.PDF_JAVASCRIPT = false;
NutrientViewer.load(configuration);

```

## Validating forms

Form field validation consists of ensuring that the value of a form field is always correct.

Form fields and their corresponding widget annotations can be extended with Action Scripts, which can then be used to perform form field validation: Initialize a [`NutrientViewer.Actions.JavaScriptAction`](https://www.nutrient.io/api/web/classes/NutrientViewer.Actions.JavaScriptAction.html) with a script, and associate it with a form field and a particular trigger event, like `onInput`, which is triggered when something is typed in a form field.

The following example adds a validation script that will prevent the user from entering the letter `a` in the new form field:

```js

const widget = new NutrientViewer.Annotations.WidgetAnnotation({
  id: NutrientViewer.generateInstantId(),
  pageIndex: 0,
  boundingBox: new NutrientViewer.Geometry.Rect({
    left: 100,
    top: 150,
    width: 80,
    height: 24
  }),
  formFieldName: "MyValidatedFormField",
  additionalActions: {
    onInput: new NutrientViewer.Actions.JavaScriptAction({
      script:
        "if (event.change!= 'a') { event.rc = true; } else { event.rc = false; }"
    })
  }
});
const formField = new NutrientViewer.FormFields.TextFormField({
  name: "MyValidatedFormField",
  annotationIds: NutrientViewer.Immutable.List([widget.id])
});
instance.create([widget, formField]);

```

For more information about PDF actions, read our [PDF actions](https://www.nutrient.io/guides/web/annotations/pdf-actions.md) guide.

You can set regular action scripts in Adobe Acrobat too. Open the Prepare Form tool and double-click on a form field. Click on the Actions tab and choose Run a JavaScript from the Select Action dropdown box.

## How to create a JavaScript-enabled PDF document using Adobe Acrobat

## Supported triggers

Nutrient Web SDK supports running JavaScript actions from the following triggers.

### WidgetAnnotation

- [Focus](https://www.nutrient.io/api/web/classes/NutrientViewer.Annotations.WidgetAnnotation.html#additionalactions)

- [Blur](https://www.nutrient.io/api/web/classes/NutrientViewer.Annotations.WidgetAnnotation.html#additionalactions)

- [Input](https://www.nutrient.io/api/web/classes/NutrientViewer.Annotations.WidgetAnnotation.html#additionalactions)

- [Format](https://www.nutrient.io/api/web/classes/NutrientViewer.Annotations.WidgetAnnotation.html#additionalactions)

### FormField

- [Calculate](https://www.nutrient.io/api/web/classes/NutrientViewer.FormFields.FormField.html#additionalactions)

- [Change](https://www.nutrient.io/api/web/classes/NutrientViewer.FormFields.FormField.html#additionalactions)

- [Format](https://www.nutrient.io/api/web/classes/NutrientViewer.FormFields.FormField.html#additionalactions)

- [Input](https://www.nutrient.io/api/web/classes/NutrientViewer.FormFields.FormField.html#additionalactions)

## Supported features

Nutrient Web SDK has basic support for the most common JavaScript API methods and properties, detailed below.

Each of the methods listed below may support only a subset of the features and parameters detailed in the specification. We therefore advise you to test your JavaScript-enabled documents thoroughly. [Contact Support](https://support.nutrient.io/hc/en-us/requests/new) if you run into issues or limitations.

### App

- `alert`, `launchURL`, `viewerVersion`

### Console

- `println`

### Doc

- `getField`, `removeField`, `mailDoc`, `getNthFieldName`, `resetForm`, `print`

- `numFields`, `pageNum`, `info`, `gotoNamedDest`

### Util

- `printx`, `printd`, `printf`

### Color

- `convert`, `transparent`, `black`, `white`, `red`, `green`, `blue`, `cyan`, `magenta`, `yellow`, `dark gray`, `gray`, `light gray`, custom color arrays

### Event

- `value`, `rc`, `selStart`, `selEnd`, `willCommit`, `target`, `change`, `name`, `type`

### Field

- `getArray`, `checkThisBox`, `isBoxChecked`, `getItemAt`, `setItems`, `clearItems`, `setItems`, `insertItemAt`, `deleteItemAt`, `setAction`, `buttonImportIcon`

- `name`, `value`, `textColor`, `fillColor`, `strokeColor`, `readonly`, `exportValues`, `currentValueIndices`, `multipleSelection`, `commitOnSelChange`, `numItems`, `hidden`, `editable`, `type`, `page`, `borderStyle`, `rotation`, `defaultValue`, `doNotSpellCheck`, `userName`, `alignment`, `rect`, `doc`, `required`, `display`, `calcOrderIndex`, `comb`, `doNotScroll`, `richText`, `multiline`, `fileSelect`, `password`, `charLimit`

Various functions for formatting, validation, and calculation are also supported. You can read [Adobe’s documentation](https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsdevguide/JS_Dev_AcrobatForms.html) to learn more about them.

### Number formatting

- `AFNumber_Format`, `AFNumber_Keystroke`, `AFMakeNumber`

### Percent formatting

- `AFPercent_Format`, `AFPercent_Keystroke`

### Date formatting

- `AFDate_Format`, `AFDate_FormatEx`, `AFDate_Keystroke`, `AFDate_KeystrokeEx`, `AFTime_Format`, `AFTime_FormatEx`, `AFTime_Keystroke`

### Special formatting

- `AFSpecial_Format`, `AFSpecial_Keystroke`, `AFSpecial_KeystrokeEx`

### Simple Formatting

- `AFSimple`, `AFSimple_Calculate`

### Range validation

- `AFRange_Validate`

## Debugging

If there’s a problem with the JavaScript in a document, it’s a good idea to first check that the scripts don’t contain syntax or logic errors. To see all JavaScript code used in a document:

- Open Adobe Acrobat.

- Click Tools.

- Select Prepare Form.

- Click the small down arrow on the sidebar.

- Choose All JavaScripts.

The screenshots below show how the All JavaScripts window appears in Adobe Acrobat.

### JavaScript logs

If JavaScript is enabled, Nutrient will log errors to the console if there’s a problem with the JavaScript code. We tried to make diagnostics as specific as possible to help you debug your documents more easily. For example, the following error message is logged if the script creator mistakenly writes `this.getField(4);`:

```sh

Catalog[58188:7996163] [JavaScript] [Error] Error: Argument name 'cName' has wrong type. Argument has type number, but expected string.

```

As you can read in [the API documentation](http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf), `getField`’s `cName` argument must be a string.

In this other example, the script contains one API property that we currently don’t support:

```sh

Catalog[58188:7996163] [JavaScript] [Error] TypeError: undefined not callable (property 'notSupportedAPI' of [object global])

```

## Learn more

For more information, refer to the following:

- [JavaScript™ for Acrobat® API Reference (Acrobat 8)](http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf)

- [Developing Acrobat® Applications Using JavaScript™](http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_developer_guide.pdf)

- [Acrobat JavaScript Object Specification](http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/5186AcroJS.pdf)

- [Adobe DevNet JavaScript Overview](https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsdevguide/JS_Dev_AcrobatForms.html)
---

## Related pages

- [Extract data from PDF form fields using JavaScript](/guides/web/forms/extract-form-data.md)
- [JavaScript PDF fillable form library](/guides/web/forms.md)
- [Master PDF actions with JavaScript integration](/guides/web/forms/pdf-actions-support.md)
- [Read PDF form fields using JavaScript](/guides/web/forms/read-form-fields.md)
- [Flatten dynamic PDF forms using JavaScript](/guides/web/forms/flatten.md)

