Skip to content

Commit bfd65ab

Browse files
github-actions[bot]KB Botntachevadimodi
authored
Merge new-kb-inputs-disable-copy-paste-0d4b5ce49e56413e8e3b628f5aacd23d-2540 into production (#2573)
* Added new kb article inputs-disable-copy-paste * Update knowledge-base/inputs-disable-copy-paste.md * Update knowledge-base/inputs-disable-copy-paste.md Co-authored-by: Dimo Dimov <[email protected]> * Update knowledge-base/inputs-disable-copy-paste.md Co-authored-by: Dimo Dimov <[email protected]> * Update knowledge-base/inputs-disable-copy-paste.md Co-authored-by: Dimo Dimov <[email protected]> * Update knowledge-base/inputs-disable-copy-paste.md Co-authored-by: Dimo Dimov <[email protected]> * Update knowledge-base/inputs-disable-copy-paste.md Co-authored-by: Dimo Dimov <[email protected]> * Update knowledge-base/inputs-disable-copy-paste.md Co-authored-by: Dimo Dimov <[email protected]> --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Nadezhda Tacheva <[email protected]> Co-authored-by: Dimo Dimov <[email protected]>
1 parent c9f6359 commit bfd65ab

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: Disable Copy and Paste in Telerik Blazor Inputs
3+
description: A guide that shows how to prevent 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 and paste actions in a NumericTextBox?
39+
- Can I use JavaScript Interop to control copy and paste in the Telerik 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 input = document.querySelector(".no-copy-paste input");
62+
63+
if (input) {
64+
input.oncopy = e => e.preventDefault();
65+
input.onpaste = e => e.preventDefault();
66+
}
67+
}
68+
</script>
69+
70+
@code {
71+
private string TBValue { get; set; }
72+
73+
protected override async Task OnAfterRenderAsync(bool firstRender)
74+
{
75+
if (firstRender)
76+
{
77+
// ensure the HTML is rendered in the browser
78+
await Task.Delay(1);
79+
80+
// prevent copy and paste in the textbox
81+
await js.InvokeVoidAsync("preventCutCopyPaste");
82+
}
83+
84+
await base.OnAfterRenderAsync(firstRender);
85+
}
86+
}
87+
````
88+
89+
## See Also
90+
91+
- [Using JavaScript Interop in Blazor](https://docs.microsoft.com/aspnet/core/blazor/javascript-interop)

0 commit comments

Comments
 (0)