HTML onpaste Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onpaste attribute is an inline event handler that runs JavaScript when the paste event fires. That happens when the user pastes content — with Ctrl+V (Cmd+V on Mac), Edit → Paste, or a context menu Paste action. Use it to validate pasted text, transform formatting, strip unwanted characters, or show feedback. Read clipboard data with event.clipboardData.getData("text"). Call event.preventDefault() when you replace the default paste. For production, prefer addEventListener("paste", …).

What You’ll Learn

01

Event handler

Inline JS.

02

paste

Insert text.

03

clipboardData

Read paste.

04

preventDefault

Custom insert.

05

+ oncopy

Clipboard trio.

06

Validation

Filter input.

Purpose of onpaste Attribute

The primary purpose of onpaste is to react when the user inserts clipboard content into an editable field — so you can enforce rules (numbers only, max length), normalize text (uppercase emails), or log analytics without waiting for an input event after the fact.

The paste event fires on the element that receives the paste — typically <input>, <textarea>, or a contenteditable region. It bubbles, so a parent can listen for pastes in multiple fields.

💡
Use paste for clipboard control

To change what gets pasted, call event.preventDefault(), read clipboardData, then insert your modified text manually.

📝 Syntax

Set onpaste on an editable element or assign element.onpaste in JavaScript:

onpaste.html
<input type="text" onpaste="handlePaste(event)">

<textarea onpaste="handlePaste(event)"></textarea>

<script>
  field.onpaste = handlePaste;
</script>

Syntax Rules

  • Value is JavaScript executed when the paste event fires.
  • Pass event to access clipboardData.
  • Works on editable elements: input, textarea, contenteditable.
  • Use event.preventDefault() before custom insertion.
  • The event bubbles — listen on a parent for multiple fields.
  • Modern alternative: element.addEventListener("paste", handler).
  • Do not block paste without good reason — it frustrates users.

💎 Values

The onpaste attribute accepts a string of JavaScript code:

  • onpaste="handlePaste(event)" — Call a function with the event.
  • onpaste="logPaste(event)" — Show feedback on paste.
  • JavaScript: input.onpaste = (e) => { … } — property assignment.
onpaste-handler.html
function handlePaste(event) {
  event.preventDefault();
  const pasted = event.clipboardData.getData("text");
  const cleaned = pasted.replace(/\D/g, ""); // digits only
  event.target.value = cleaned;
}

⚡ Quick Reference

Property / APIWhen / howNotes
pasteUser pastes contentonpaste
event.clipboardDataDuring paste handlerRead clipboard
getData("text")Plain text from clipboardMost common
preventDefault()Before custom insertBlocks default paste
Typical elementsinput, textareaEditable targets

Applicable Elements

ElementSupported?Notes
<input>Yestext, email, search, etc.
<textarea>YesMulti-line paste
contenteditableYesRich text editors
<div> (plain)NoNot editable by default
<body>RareOnly if focus allows paste

onpaste vs oncopy vs oncut

AttributeEventTypical use
onpastepasteValidate / transform inserted text
oncopycopyModify copied content
oncutcutHandle cut + remove
oninputinputAny value change (incl. typing)

Examples Gallery

Uppercase on paste, dynamic handler assignment, and digits-only validation.

👀 Live Preview

Paste text below — the handler shows what was pasted:

Waiting for paste…

Example — Convert pasted text to uppercase

Prevent default paste and insert transformed text:

onpaste-uppercase.html
<input type="text" id="pasteInput" onpaste="handlePaste(event)">

<script>
  function handlePaste(event) {
    event.preventDefault();
    const pasted = event.clipboardData.getData("text");
    document.getElementById("pasteInput").value =
      pasted.toUpperCase();
  }
</script>
Try It Yourself

How It Works

preventDefault() stops the normal paste. You read clipboard text, transform it, and set the input value yourself.

Dynamic Values with JavaScript

Assign element.onpaste after the page loads:

dynamic-onpaste.html
<input type="text" id="dynamicPasteField">

<script>
  document.getElementById("dynamicPasteField").onpaste =
    function (event) {
      document.getElementById("log").textContent =
        "Paste detected at " +
        new Date().toLocaleTimeString();
    };
</script>
Try It Yourself

How It Works

Property assignment on the input is equivalent to an inline onpaste attribute for the paste event.

Example — Digits-only paste validation

Strip non-numeric characters from pasted phone numbers:

onpaste-digits.html
function digitsOnlyPaste(event) {
  event.preventDefault();
  const pasted = event.clipboardData.getData("text");
  event.target.value = pasted.replace(/\D/g, "");
}
Try It Yourself

How It Works

Validate at paste time so invalid characters never appear in the field — cleaner than fixing them after an input event.

♿ Accessibility

  • Do not block paste unnecessarily — Paste is essential for many users, including assistive technology users.
  • Explain transformations — If paste changes text, show a brief status message.
  • Keep fields paste-friendly — Avoid stripping content users expect (password managers, autofill).
  • Use visible error text — Not color alone when paste is rejected.
  • Prefer validation over blocking — Clean input rather than silently failing.

🧠 How onpaste Works

1

User copies text

Clipboard ready.

Copy
2

Ctrl+V in field

Focus on input.

Paste
3

paste fires

onpaste runs.

Event
=

Insert / validate

Transform text.

Browser Support

The paste event and onpaste handler are supported in all modern browsers. clipboardData during paste is available in current Chrome, Firefox, Safari, and Edge.

Clipboard Events · Fully supported

Universal onpaste support

All major browsers fire paste on editable elements when the user inserts clipboard content.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onpaste attribute 99% supported

Bottom line: Reliable for paste validation and transformation in all modern browsers.

💡 Best Practices

✅ Do

  • Pass event to access clipboardData
  • Use preventDefault() when transforming pasted text
  • Prefer addEventListener("paste", …) in production
  • Validate early — at paste time, not only on submit
  • Pair with oncopy / oncut for full clipboard UX

❌ Don’t

  • Block all paste without a clear reason
  • Use alert() on every paste
  • Put onpaste on non-editable elements
  • Assume rich HTML paste — use getData("text") for plain text
  • Rely on paste alone — also validate on input for typed text

Conclusion

The onpaste attribute runs JavaScript when the user pastes content into an editable field — ideal for validation, formatting, and clipboard-aware form UX.

Read event.clipboardData, use preventDefault() when customizing insertion, and prefer addEventListener in production code.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onpaste

Bookmark these before handling clipboard paste in forms.

5
Core concepts
📄 02

Editable only

input, area.

Scope
🔎 03

clipboardData

Read text.

API
🔄 04

Transform

preventDefault.

Pattern
05

Don’t block

a11y first.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the paste event fires — when the user pastes clipboard content into an editable element.
input, textarea, and contenteditable elements — anything that can receive pasted text.
event.clipboardData.getData("text") inside your paste handler.
When you want to replace the default paste with your own transformed or filtered text.
element.addEventListener("paste", handler) is preferred for production code.
Yes in all modern browsers; Internet Explorer 9+.

Handle paste like a pro

Practice the onpaste attribute with uppercase and validation examples in the Try It editor.

Try onpaste demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful