Skip to content
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

Can't disable paste #56

Closed
plmpradeep opened this issue Sep 3, 2021 · 5 comments
Closed

Can't disable paste #56

plmpradeep opened this issue Sep 3, 2021 · 5 comments

Comments

@plmpradeep
Copy link

plmpradeep commented Sep 3, 2021

I want to disable paste. But using (paste)="false" with digitOnly directive doesn't work.

   <input [(ngModel)]="test" placeholder="Enter Value" type="text" digitOnly
                         [pattern]="getInputPattern()" (input)="onInputChange()" (paste)="false">
@billn6
Copy link
Contributor

billn6 commented Sep 3, 2021

That did not work for me either, nor did onPaste="return false;" nor trying event.preventDefault(); as suggested here.

The following did work. It allows you to specify allowPaste=false on the input control.

add this

@input() allowPaste = true;

and then modify the onPaste as follows

@HostListener('paste', ['$event'])
onPaste(event: any): void {
if (this.allowPaste === true) {
let pastedInput: string = '';
if ((window as { [key: string]: any })['clipboardData']) {
// Browser is IE
pastedInput = (window as { [key: string]: any })['clipboardData'].getData(
'text'
);
} else if (event.clipboardData && event.clipboardData.getData) {
// Other browsers
pastedInput = event.clipboardData.getData('text/plain');
}

  this.pasteData(pastedInput);
  event.preventDefault();
} else {  // this prevents the paste 
  event.preventDefault();
  event.stopPropagation();
}

}

@changhuixu
Copy link
Owner

Hi @billn6 , I think your solution works.

@plmpradeep , what's your opinion on this one (adding a new input field to control the paste handler)?

@plmpradeep
Copy link
Author

Yup. Suggested changes work fine.

@patrykdawid
Copy link

@changhuixu Will you throw a small but needed change into the 2.x branch as well?

@changhuixu
Copy link
Owner

Hi @patrykdawid
A v2.4.0 is released. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants