Reputation: 2543
I have a material input control, i have restrict the special character while user enter, but when type some words in any editor and copy and paste the words with special character, which is not working.
I have write the directive for that to prevent special character,but can one provide the better solution restrict in copy paste.
app.component.html:
<form class="example-form">
<mat-form-field class="example-full-width">
<input matInput specialIsAlphaNumeric placeholder="Favorite food" value="Sushi">
</mat-form-field>
<mat-form-field class="example-full-width">
<textarea matInput placeholder="Leave a comment"></textarea>
</mat-form-field>
</form>
directive:
import { Directive, HostListener, Input } from '@angular/core';
@Directive({
selector: '[specialIsAlphaNumeric]'
})
export class SpecialCharacterDirective {
regexStr = '^[a-zA-Z0-9_]*$';
@Input() isAlphaNumeric: boolean;
@HostListener('keypress', ['$event']) onKeyPress(event) {
return new RegExp(this.regexStr).test(event.key);
}
}
https://stackblitz.com/edit/angular-cijbqy-biwrck?file=app%2Finput-overview-e[stackblit
Steps to reproduce:
type it special character which is not allowed : working fine. while copy paste wit allows special character
Upvotes: 10
Views: 28851
Reputation: 467
The following approach works without using the setTimeout call, which means there is no flashing of special characters in the input control when the other response by Aashish K Jha is used
import { Directive, HostListener, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[specialIsAlphaNumeric]'
})
export class SpecialCharacterDirective {
regexStr = '^[a-zA-Z0-9_]*$';
@Input() isAlphaNumeric: boolean;
constructor(private el: ElementRef) { }
@HostListener('keypress', ['$event']) onKeyPress(event) {
return new RegExp(this.regexStr).test(event.key);
}
@HostListener('paste', ['$event']) blockPaste(event: ClipboardEvent) {
this.validateFields(event);
}
validateFields(event: ClipboardEvent) {
event.preventDefault();
const pasteData = event.clipboardData.getData('text/plain').replace(/[^a-zA-Z0-9 ]/g, '');
document.execCommand('insertHTML', false, pasteData);
}
}
Upvotes: 3
Reputation: 71
You can use Ng Knife utility
Import NgKnifeModule
...
import { NgKnifeModule } from 'ng-knife';
...
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgKnifeModule
],
...
});
export class AppModule { }
Using it:
<!-- Aphanumeric -->
<input knifeAlphanumeric type="text">
Upvotes: 1
Reputation: 7231
Try Like this:
import { Directive, HostListener, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[specialIsAlphaNumeric]'
})
export class SpecialCharacterDirective {
regexStr = '^[a-zA-Z0-9_]*$';
@Input() isAlphaNumeric: boolean;
constructor(private el: ElementRef) { }
@HostListener('keypress', ['$event']) onKeyPress(event) {
return new RegExp(this.regexStr).test(event.key);
}
@HostListener('paste', ['$event']) blockPaste(event: KeyboardEvent) {
this.validateFields(event);
}
validateFields(event) {
setTimeout(() => {
this.el.nativeElement.value = this.el.nativeElement.value.replace(/[^A-Za-z ]/g, '').replace(/\s/g, '');
event.preventDefault();
}, 100)
}
}
Upvotes: 29