Maryam Faheem
Maryam Faheem

Reputation: 37

Alpha Numeric Directive is not working for Special Characters



ALPHANUMERIC.DIRECTIVE.TS FILE

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

@Directive({
selector: '[appAlphaNumeric]'
})
export class AlphaNumericDirective {
constructor() { }

keyPressAlphanumeric(event) {
var inp = String.fromCharCode(event.keyCode);

if (/[a-zA-Z0-9]/.test(inp)) {
  return true;
} 

else {
  event.preventDefault();
  return false;
}
}
}

HTML FILE

<!-- Employee Number -->
<mat-form-field appearance="outline">
<mat-label>Employee Number</mat-label>

<input formControlName ="employeenumber" matInput placeholder="Placeholder" 
 appAlphaNumeric >

</mat-form-field>   

Upvotes: 0

Views: 499

Answers (0)

Related Questions