Contact Form Submission Validator and MongoDB Logger - n8n Workflow

Secure your contact form submissions. This n8n workflow validates input against injection attempts, cleans data, and logs validated submissions directly to MongoDB Atlas using custom JavaScript.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Developers needing robust data validation mechanisms built into their backend.

  • Businesses looking for reliable n8n templates to handle high-volume web form submissions securely.

  • System administrators requiring secure logging of user inputs directly into a MongoDB database.

  • Anyone implementing an n8n workflow that requires custom scripting for data integrity checks.

Overview

This robust n8n workflow provides a comprehensive, secure solution for handling web form submissions. It immediately solves the critical problem of data security and integrity by implementing a custom JavaScript validation layer directly after the initial capture. Before storing any data in MongoDB, the n8n node checks for malicious patterns (such as SQL injection characters or XSS attempts) and ensures contact fields, like phone numbers, adhere to proper formats. This specific n8n workflow is designed for efficiency and safety, ensuring that only clean, standardized data reaches your MongoDB Atlas database, making it a valuable addition to available n8n templates for secure backend operations. The reliability of this n8n automation means you spend less time manually scrubbing input and more time analyzing verified data.

How it Works

The entire process is streamlined through several interconnected n8n nodes, beginning with an essential form trigger.


  1. Trigger Reception: The flow starts with the "On form submission" n8n trigger, which acts as the entry point. It instantly activates when a user fills out the contact form, capturing fields like Name, Email, and Phone Number.

  2. Custom Validation (Security Layer): The flow proceeds immediately to the "Validate Pattern" custom Code n8n node. This step is crucial, as it executes specialized JavaScript logic to test the Phone Number against a robust regex pattern and, more importantly, checks fields like Name and Email for dangerous characters commonly associated with SQL or XSS injection attacks. If a field fails validation, the n8n workflow marks the data as "Is not Valid," preventing compromised data from moving forward.

  3. Data Transformation: The "Edit Fields" n8n node handles data standardization. It cleans up the structure by converting field names from human-readable formats (e.g., "Phone Number") to the database-friendly snakecase format (name, lastname, phone).

  4. Database Insertion: The MongoDB n8n node ("Insert documents") securely connects and logs the validated, transformed document into the designated contact collection in your MongoDB database.

  5. Confirmation: Finally, the "Form Ending" n8n node provides a clean conclusion to the process by displaying a custom success message back to the user who initiated the original n8n trigger.

Installation Guide

To deploy this n8n workflow and start securely processing contact forms, follow these steps:


  1. Import: Download the provided n8n workflow JSON file and import it directly into your self-hosted or cloud n8n instance.

  2. MongoDB Credentials: You must configure the MongoDB n8n node ("Insert documents"). Click on the node and establish your credentials, typically by providing the connection URL for your MongoDB Atlas cluster.

  3. Trigger Setup: Activate the "On form submission" n8n trigger. Once the n8n workflow is saved and active, the trigger will generate a unique webhook URL. Use this URL as the target endpoint for your website’s contact form submission action.

  4. Testing: Always execute the n8n workflow manually by submitting a test form entry to ensure the complex validation logic within the Code n8n node is functioning as intended, thereby verifying the integrity of this n8n template before deploying it to production.

Node Details

On form submission (Form Trigger n8n trigger):
Function: Acts as the starting n8n node, listening for incoming POST requests from a web contact form, initiating the n8n workflow.
Key Configuration: Configured to capture fields: Name, Last Name, Email, and Phone Number.
Validate Pattern (Code n8n node):
Function: Executes custom JavaScript to enforce data integrity and prevent security breaches. It checks the phone number against a regex pattern and verifies Name, Last Name, and Email fields for dangerous characters (e.g., drop, script, ', ;, --).
Key Configuration: Contains the core logic for security scrubbing, tagging non-compliant data as "Is not Valid".
Edit Fields (Set n8n node):
Function: A data transformation n8n node that standardizes the field names (e.g., converting "Phone Number" to phone) to ensure consistent data structure for the MongoDB insertion.
Insert documents (MongoDB n8n node):
Function: The crucial storage action. It inserts the clean, validated data object into the 'contact' collection in the connected MongoDB database.
Key Configuration: Operation set to insert, targeting the contact collection.
Form Ending (Form n8n node):
* Function: Concludes the user interaction by displaying a confirmation message, notifying the user that their contact details have been successfully processed by the n8n workflow.

Related n8n Workflows

Free

Nodes: 6 Nodes
Updated: December 26 2025
View all
Created by

I like to believe that I can contribute to creating a better world with the developments I make :)

Featured*