Syncfusion AI Assistant

How can I help you?

Validate PDF Form Fields in ASP.NET Core PDF Viewer

28 Feb 20266 minutes to read

The Syncfusion ASP.NET Core PDF Viewer provides built-in support for validating form fields before printing, downloading, or submitting a PDF document. Validation ensures all required form fields are filled, helping enforce data completeness and improving the reliability of collected data.

How PDF Form Validation Works

Form field validation follows this flow:

  • Enable validation using the enableFormFieldsValidation property.
  • Handle the validateFormFields event to determine which required fields remain unfilled.
  • When validation is enabled and the user attempts to print, download, or submit:
    • The validateFormFields event is triggered.
    • Unfilled required fields are listed in args.nonFillableFields.
    • Cancel the action, show an error message, or focus an invalid field as needed.

Enable PDF Form Field Validation

To enable validation, set the enableFormFieldsValidation property to true and wire the validateFormFields event.

<div class="text-center">
    <ejs-pdfviewer id="pdfviewer" style="height:600px" resourceUrl="https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib" documentPath="https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf">
    </ejs-pdfviewer>
</div>

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
    var viewerElement = document.getElementById('pdfviewer');
    var pdfviewer = viewerElement && viewerElement.ej2_instances && viewerElement.ej2_instances[0];
    if (!pdfviewer) return;

    // 1) Default for new Textbox fields
    pdfviewer.textFieldSettings = { isRequired: true };

    // 2) Validation wiring
    pdfviewer.enableFormFieldsValidation = true;
    pdfviewer.validateFormFields = function (args) {
      if (args && args.formField && args.formField.length > 0) {
        alert('Please fill all required fields. Missing: ' + args.formField[0].name);
      }
    };

    // 3) Creation (add a Textbox form field once the document is loaded)
    pdfviewer.documentLoad = function () {
      pdfviewer.formDesignerModule.addFormField('Textbox', {
        name: 'Email',
        bounds: { X: 146, Y: 260, Width: 220, Height: 24 },
        isRequired: true,
        tooltip: 'Email is required'
      });
    };
});
</script>

Mark Fields as Required

Only fields marked as required participate in validation. Use the isRequired property when creating or updating form fields.

<script type="text/javascript">
// Creation of a required textbox (inside documentLoad) that blocks printing until it is filled.
document.addEventListener('DOMContentLoaded', function () {
  var viewerElement = document.getElementById('pdfviewer');
  var pdfviewer = viewerElement && viewerElement.ej2_instances && viewerElement.ej2_instances[0];
  if (!pdfviewer) return;

  pdfviewer.documentLoad = function () {
    pdfviewer.formDesignerModule.addFormField('Textbox', {
      name: 'Email',
      bounds: { X: 146, Y: 260, Width: 220, Height: 24 },
      isRequired: true,
      tooltip: 'Email is required'
    });
  };
});
</script>

Handle PDF Form Validation Results

In the validateFormFields event, control the behavior when fields are missing. Typical actions include:

  • Cancel the print or download operation.
  • Display an error message to the user.
  • Focus the first unfilled required field.
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
  var viewerElement = document.getElementById('pdfviewer');
  var pdfviewer = viewerElement && viewerElement.ej2_instances && viewerElement.ej2_instances[0];
  if (!pdfviewer) return;

  pdfviewer.enableFormFieldsValidation = true;
  pdfviewer.validateFormFields = function (args) {
    if (args && args.formField && args.formField.length > 0) {
      // Example: prevent the pending action and notify the user
      args.cancel = true;
      alert('Please fill all required fields. Missing: ' + args.formField[0].name);
      // Optionally focus or scroll to the field
    }
  };
});
</script>

Tips

  • Use isRequired to mark mandatory fields clearly.
  • Validation is triggered only during print, download, or submit actions.
  • For custom validation logic (such as validating email format):
    • Retrieve all form fields using retrieveFormFields().
    • Apply custom checks before allowing the action to proceed.

See Also