-
Notifications
You must be signed in to change notification settings - Fork 81
Added new kb article inputs-disable-copy-paste #2540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ntacheva
merged 8 commits into
master
from
new-kb-inputs-disable-copy-paste-0d4b5ce49e56413e8e3b628f5aacd23d
Nov 26, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f98c5c8
Added new kb article inputs-disable-copy-paste
7565bcf
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva 12a47d4
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva cd4487c
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva fceb3a3
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva 657d8eb
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva 850da0d
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva 5e57b05
Update knowledge-base/inputs-disable-copy-paste.md
ntacheva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| --- | ||
| title: Disable Copy and Paste in Telerik Blazor Inputs | ||
| description: A guide that shows how to prevent users from copying and pasting text into the Telerik Blazor Inputs in a Blazor application. | ||
| type: how-to | ||
| page_title: How to Prevent Copy and Paste Actions in Telerik Blazor Inputs | ||
| slug: inputs-kb-disable-copy-paste | ||
| tags: autocomplete, combobox, datepicker, daterangepicker, datetimepicker, multicolumncombobox, numerictextbox, textbox, textarea, timepicker, blazor, copy, paste | ||
| res_type: kb | ||
| ticketid: 1670453 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td> | ||
| AutoComplete for Blazor,<br /> | ||
| ComboBox for Blazor,<br /> | ||
| DatePicker for Blazor,<br /> | ||
| DateRangePicker for Blazor,<br /> | ||
| DateTimePicker for Blazor,<br /> | ||
| MultiColumnComboBox for Blazor,<br /> | ||
| NumericTextBox for Blazor,<br /> | ||
| TextArea for Blazor,<br /> | ||
| TextBox for Blazor,<br /> | ||
| TimePicker for Blazor | ||
| </td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| This KB article answers the following questions: | ||
| - How can I prevent users from pasting text into a TextBox? | ||
| - What is the method to disable the copy and paste actions in a NumericTextBox? | ||
| - Can I use JavaScript Interop to control copy and paste in the Telerik ComboBox for Blazor? | ||
|
|
||
| ## Solution | ||
|
|
||
| To disable the copy and paste functionality in a TextBox and other Telerik Blazor inputs, follow the steps below: | ||
|
|
||
| 1. Add a custom CSS class to the component to ensure you are targeting this specific instance. | ||
|
|
||
| 2. Create a JavaScript function that targets the input of your component and prevents the default `oncopy` and `onpaste` events. | ||
|
|
||
| 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. | ||
|
|
||
| ````CSHTML | ||
| @inject IJSRuntime js | ||
|
|
||
| <TelerikTextBox @bind-Value="@TBValue" | ||
| Width="300px" | ||
| Class="no-copy-paste" /> | ||
|
|
||
| @* Move JavaScript code to a separate JS file in production *@ | ||
| <script suppress-error="BL9992"> | ||
| function preventCutCopyPaste() { | ||
| var input = document.querySelector(".no-copy-paste input"); | ||
|
|
||
| if (input) { | ||
| input.oncopy = e => e.preventDefault(); | ||
| input.onpaste = e => e.preventDefault(); | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| @code { | ||
| private string TBValue { get; set; } | ||
|
|
||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| if (firstRender) | ||
| { | ||
| // ensure the HTML is rendered in the browser | ||
| await Task.Delay(1); | ||
|
|
||
| // prevent copy and paste in the textbox | ||
| await js.InvokeVoidAsync("preventCutCopyPaste"); | ||
| } | ||
|
|
||
| await base.OnAfterRenderAsync(firstRender); | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Using JavaScript Interop in Blazor](https://docs.microsoft.com/aspnet/core/blazor/javascript-interop) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.