👀 Live Preview
Paste text below — the handler shows what was pasted:
Waiting for paste…

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", …).
Inline JS.
Insert text.
Read paste.
Custom insert.
Clipboard trio.
Filter input.
onpaste AttributeThe 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.
To change what gets pasted, call event.preventDefault(), read clipboardData, then insert your modified text manually.
Set onpaste on an editable element or assign element.onpaste in JavaScript:
<input type="text" onpaste="handlePaste(event)">
<textarea onpaste="handlePaste(event)"></textarea>
<script>
field.onpaste = handlePaste;
</script>paste event fires.event to access clipboardData.input, textarea, contenteditable.event.preventDefault() before custom insertion.element.addEventListener("paste", handler).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.input.onpaste = (e) => { … } — property assignment.function handlePaste(event) {
event.preventDefault();
const pasted = event.clipboardData.getData("text");
const cleaned = pasted.replace(/\D/g, ""); // digits only
event.target.value = cleaned;
}| Property / API | When / how | Notes |
|---|---|---|
paste | User pastes content | onpaste |
event.clipboardData | During paste handler | Read clipboard |
getData("text") | Plain text from clipboard | Most common |
preventDefault() | Before custom insert | Blocks default paste |
| Typical elements | input, textarea | Editable targets |
| Element | Supported? | Notes |
|---|---|---|
<input> | Yes | text, email, search, etc. |
<textarea> | Yes | Multi-line paste |
contenteditable | Yes | Rich text editors |
<div> (plain) | No | Not editable by default |
<body> | Rare | Only if focus allows paste |
onpaste vs oncopy vs oncut| Attribute | Event | Typical use |
|---|---|---|
onpaste | paste | Validate / transform inserted text |
oncopy | copy | Modify copied content |
oncut | cut | Handle cut + remove |
oninput | input | Any value change (incl. typing) |
Uppercase on paste, dynamic handler assignment, and digits-only validation.
Paste text below — the handler shows what was pasted:
Waiting for paste…
Prevent default paste and insert transformed text:
<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>preventDefault() stops the normal paste. You read clipboard text, transform it, and set the input value yourself.
Assign element.onpaste after the page loads:
<input type="text" id="dynamicPasteField">
<script>
document.getElementById("dynamicPasteField").onpaste =
function (event) {
document.getElementById("log").textContent =
"Paste detected at " +
new Date().toLocaleTimeString();
};
</script>Property assignment on the input is equivalent to an inline onpaste attribute for the paste event.
Strip non-numeric characters from pasted phone numbers:
function digitsOnlyPaste(event) {
event.preventDefault();
const pasted = event.clipboardData.getData("text");
event.target.value = pasted.replace(/\D/g, "");
}Validate at paste time so invalid characters never appear in the field — cleaner than fixing them after an input event.
Clipboard ready.
Focus on input.
onpaste runs.
Transform text.
The paste event and onpaste handler are supported in all modern browsers. clipboardData during paste is available in current Chrome, Firefox, Safari, and Edge.
onpaste supportAll major browsers fire paste on editable elements when the user inserts clipboard content.
Bottom line: Reliable for paste validation and transformation in all modern browsers.
event to access clipboardDatapreventDefault() when transforming pasted textaddEventListener("paste", …) in productiononcopy / oncut for full clipboard UXalert() on every pasteonpaste on non-editable elementsgetData("text") for plain textinput for typed textThe 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.
onpasteBookmark these before handling clipboard paste in forms.
Ctrl+V.
Triggerinput, area.
ScopeRead text.
APIpreventDefault.
Patterna11y first.
Gotchapaste 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.element.addEventListener("paste", handler) is preferred for production code.Practice the onpaste attribute with uppercase and validation examples in the Try It editor.
5 people found this page helpful