23

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>
jitender
  • 10,238
  • 1
  • 18
  • 44
vishnu
  • 4,377
  • 15
  • 52
  • 89
  • 1
    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: https://security.stackexchange.com/questions/131106/is-there-any-reason-to-disable-paste-password-on-login – Thomas Mulder Jun 14 '19 at 13:43
  • @ThomasMulder me too, I just shared workarounds to bypass this behavior if the app uses pure JavaScript events: https://stackoverflow.com/questions/4386300/javascript-dom-how-to-remove-all-events-of-a-dom-object/56849582#56849582 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. – baptx Jul 02 '19 at 12:45

4 Answers4

55

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

Aamir Khan
  • 2,945
  • 2
  • 25
  • 32
15

You can do this

<input (paste)="(false)" />
Mario Linares
  • 301
  • 2
  • 3
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

jitender
  • 10,238
  • 1
  • 18
  • 44
  • 1
    Not a working solution. Cut, copy and paste actions are possible even after implementing this code. – Smokey Oct 04 '19 at 06:12
1

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