textscrubr
Home / Blog / Developers

How to Sanitize Pasted Text in a Form or App

Developers2 min readUpdated 2026-06-24
To sanitize pasted text in a form or app, normalize it on input: strip invisible and zero-width characters, normalize exotic spaces, and trim whitespace before you store or process it. Handle it in a paste event or on submit so hidden characters from users' clipboards never reach your database.

Users paste text from anywhere, chatbots, Word, web pages, and it arrives full of invisible characters and odd spacing. If you store it raw, that junk corrupts your data. Here is how to sanitize it.

Why sanitize on input

Text from a user's clipboard commonly contains:

Stored raw, these break search, comparisons, uniqueness checks, and exports later. Sanitizing at the boundary, when the text comes in, keeps your database clean and saves you from chasing invisible bugs downstream.

A basic sanitize step

A minimal normalization in JavaScript:

function sanitize(s){
  return s
    .replace(/[\u200B-\u200D\u2060\uFEFF]/g, '')  // zero-width + BOM
    .replace(/[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g, ' ') // exotic spaces -> normal
    .replace(/[\u201C\u201D]/g, '"').replace(/[\u2018\u2019]/g, "'") // smart quotes
    .replace(/[ \t]+/g, ' ')  // collapse spaces
    .trim();
}

Run it on submit, or in a paste event handler if you want to clean as the user pastes.

Handle the paste event

To clean at paste time, intercept the event, sanitize the pasted text, and insert the clean version:

field.addEventListener('paste', function(e){
  e.preventDefault();
  const text = (e.clipboardData || window.clipboardData).getData('text/plain');
  document.execCommand('insertText', false, sanitize(text));
});

This gives users immediate, clean input without changing what they see.

Sanitizing rich paste

If your field accepts rich text, also strip dangerous and messy HTML: allow a small set of semantic tags (lists, bold, headings), remove all attributes and inline styles, and unwrap everything else. This removes the Word and web clutter and protects against injected markup at the same time.

For quick fixes, use a cleaner

For a one-off or for testing what your users are pasting, run the text through a cleaner like textscrubr to see and strip the hidden characters. It is a fast way to confirm what your sanitize step should be catching.

The principle

Sanitize at the boundary. Clean text on the way in, once, and everything downstream, storage, search, export, stays clean.

See what is hidden in your text

textscrubr shows every invisible character, removes the ones with no job, and keeps your lists, headings, links and code intact. Em dash cleanup is one optional toggle. Free, and it runs entirely in your browser.

Inspect and clean my text free →

Work inside Claude or ChatGPT? The same engine is available there for a one-time $29.

Frequently asked questions

Why should I sanitize pasted text in a form?

Because users paste text with invisible characters, exotic spaces, and smart quotes that corrupt your data and break search, comparisons, and exports if stored raw. Sanitizing on input keeps your database clean.

How do I clean text in a paste event?

Intercept the paste event, read the plain text from the clipboard, run it through a sanitize function that strips invisible characters and normalizes spacing, then insert the clean version.

How do I sanitize rich pasted HTML?

Allow only a small set of semantic tags, remove all attributes and inline styles, and unwrap everything else. This strips Word and web clutter and protects against injected markup.