Skip to content

Commit f98c5c8

Browse files
author
KB Bot
committed
Added new kb article inputs-disable-copy-paste
1 parent 5492018 commit f98c5c8

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: Disabling Copy and Paste in Telerik Blazor Inputs
3+
description: A guide on preventing users from copying and pasting text into the Telerik Blazor Inputs in a Blazor application.
4+
type: how-to
5+
page_title: How to Prevent Copy and Paste Actions in Telerik Blazor Inputs
6+
slug: inputs-kb-disable-copy-paste
7+
tags: autocomplete, combobox, datepicker, daterangepicker, datetimepicker, multicolumncombobox, numerictextbox, textbox, textarea, timepicker, blazor, copy, paste
8+
res_type: kb
9+
ticketid: 1670453
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>
19+
AutoComplete for Blazor,<br />
20+
ComboBox for Blazor,<br />
21+
DatePicker for Blazor,<br />
22+
DateRangePicker for Blazor,<br />
23+
DateTimePicker for Blazor,<br />
24+
MultiColumnComboBox for Blazor,<br />
25+
NumericTextBox for Blazor,<br />
26+
TextArea for Blazor,<br />
27+
TextBox for Blazor,<br />
28+
TimePicker for Blazor
29+
</td>
30+
</tr>
31+
</tbody>
32+
</table>
33+
34+
## Description
35+
36+
This KB article answers the following questions:
37+
- How can I prevent users from pasting text into a TextBox?
38+
- What is the method to disable the copy action in a NumericTextBox?
39+
- Can I use JavaScript Interop to control copy and paste in ComboBox for Blazor?
40+
41+
## Solution
42+
43+
To disable the copy and paste functionality in a TextBox and other Telerik Blazor inputs, follow the steps below:
44+
45+
1. Add a custom CSS class to the component to ensure you are targeting this specific instance.
46+
47+
2. Create a JavaScript function that targets the input of your component and prevents the default `oncopy` and `onpaste` events.
48+
49+
3. Use JS Interop to invoke the JavaScript function during the `OnAfterRenderAsync` lifecycle method. It fires when the DOM tree is built, but before the HTML output is actually rendered in the browser. This makes it the most appropriate place to listen to and prevent the `oncopy` and `onpaste` events.
50+
51+
````CSHTML
52+
@inject IJSRuntime js
53+
54+
<TelerikTextBox @bind-Value="@TBValue"
55+
Width="300px"
56+
Class="no-copy-paste" />
57+
58+
@* Move JavaScript code to a separate JS file in production *@
59+
<script suppress-error="BL9992">
60+
function preventCutCopyPaste() {
61+
var tb = document.querySelector(".no-copy-paste input");
62+
63+
tb.oncopy = e => e.preventDefault();
64+
tb.onpaste = e => e.preventDefault();
65+
}
66+
</script>
67+
68+
@code {
69+
private string TBValue { get; set; }
70+
71+
protected override async Task OnAfterRenderAsync(bool firstRender)
72+
{
73+
if (firstRender)
74+
{
75+
await Task.Delay(1);
76+
77+
// prevent cut, copy and paste in the textbox
78+
await js.InvokeVoidAsync("preventCutCopyPaste");
79+
}
80+
81+
await base.OnAfterRenderAsync(firstRender);
82+
}
83+
}
84+
````
85+
86+
## See Also
87+
88+
- [Using JavaScript Interop in Blazor](https://docs.microsoft.com/aspnet/core/blazor/javascript-interop)

0 commit comments

Comments
 (0)