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>
You can use
Renderer
to listen tocut,copy,paste
events and callpreventDefault()
in your directive something likeThen in html
Working Demo
You can use a HostListener in your directive to catch cut, paste and copy events and then use
preventDefault()
. Here's an exampleUse directive like so
Working demo