Directive to disable Cut, Copy and Paste function for textbox using Angular2
Asked Answered
R

4

26

I am using Angular2 to restrict the copy and paste in textbox. But how do i write a custom directive, so that it will be easy to apply for all the text fields.

Below is the working code to restrict the copy and paste functionality.

<ion-input formControlName="confirmpass" type="tel" (cut)="$event.preventDefault()" (copy)="$event.preventDefault()" (paste)="$event.preventDefault()"></ion-input>
Revisal answered 20/11, 2017 at 4:9 Comment(2)
I found this question by looking for the exact opposite, if there was a way to override this horrible behavior on a site made in Angular... I know this is an old question but if anyone reads this: please do NOT prevent pasting of passwords! see the following discussion about why this is not a good idea: security.stackexchange.com/questions/131106/…Baking
@ThomasMulder me too, I just shared workarounds to bypass this behavior if the app uses pure JavaScript events: #4386800 But now I used a website that had restrictions on dates with Angular and I was not able to disable it without breaking the app so I would also be interested to know if there is a solution to disable Angular form validation and restriction.Photomechanical
A
57

You can use a HostListener in your directive to catch cut, paste and copy events and then use preventDefault(). Here's an example

import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[appBlockCopyPaste]'
})
export class BlockCopyPasteDirective {
  constructor() { }

  @HostListener('paste', ['$event']) blockPaste(e: KeyboardEvent) {
    e.preventDefault();
  }

  @HostListener('copy', ['$event']) blockCopy(e: KeyboardEvent) {
    e.preventDefault();
  }

  @HostListener('cut', ['$event']) blockCut(e: KeyboardEvent) {
    e.preventDefault();
  }
}

Use directive like so

<ion-input appBlockCopyPaste formControlName="confirmpass" type="tel"></ion-input>

Working demo

Antibiosis answered 20/11, 2017 at 5:19 Comment(4)
KeyboardEvent Deprecated developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCodeNappy
@RajaRamaMohanThavalam - keyCode is deprecated, not KeyboardEventSachiko
Not a working solution anymore. KeyboardEvent has been DeprecatedBowker
Thanks for saving my time.Huckleberry
H
21

You can do this

<input (paste)="(false)" />
Hermitage answered 25/4, 2022 at 15:45 Comment(2)
By far the cleanest solution I've seen so far. This should definitely be the approved answerMuddleheaded
Agree with @GM, this should be the ultimate answerBreuer
Z
5

You can use Renderer to listen to cut,copy,paste events and call preventDefault() in your directive something like

@Directive({ selector: '[preventCutCopyPaste]' })

export class CopyDirective {
    constructor(el: ElementRef, renderer: Renderer) {
      var events = 'cut copy paste';
      events.split(' ').forEach(e => 
      renderer.listen(el.nativeElement, e, (event) => {
        event.preventDefault();
        })
      );

    }
}

Then in html

<input type="text" preventCutCopyPaste/>

Working Demo

Zildjian answered 20/11, 2017 at 5:20 Comment(1)
Not a working solution. Cut, copy and paste actions are possible even after implementing this code.Bowker
D
1

this work for me (paste)="(false)" put this code in input field

Danieladaniele answered 14/3, 2023 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.