I want to restrict typing in a text box but I need to paste anything inside that textbox in Angular
Asked
Active
Viewed 760 times
0

Anurag Srivastava
- 14,077
- 4
- 33
- 43

Shubham Kumar Keshri
- 23
- 5
-
https://stackoverflow.com/questions/21605961/restrict-html-input-to-only-allow-paste check out this – a.prakash May 07 '22 at 18:47
1 Answers
1
You have to stop the event propagation if the combination pressed is different than the one you choose.
You can do it like this:
public onInput(event: KeyboardEvent) {
//if is pressed a different key than c or v, or the ctrl key isn't pressed stop event
if (!event.ctrlKey || (event.key.toLowerCase() !== 'c' && event.key.toLowerCase() !== 'v')) {
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
}
// else execute the default event behaviour
}
<input type="text" (keydown)="onInput($event)">
Hope it helps. ^^
P.S.: You have to use the event keydown for this to work.

Matteo Brusarosco
- 36
- 1
- 5