最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - I want to restrict input type text only Alphanumeric in Angular 9 globally - Stack Overflow

matteradmin8PV0评论

Can any one suggest me how can I add ng-pattern / directive / RegEx globally ? so that I do not want to go to each page and touch every input by adding ng-pattern or adding directive.

My application has around 200 pages , I don't want to restrict each text input with only alpha numeric.

is there any solution write mon logic that will apply for whole application ? Please suggest, I am using Angular 8.

Thank you In advance.

Can any one suggest me how can I add ng-pattern / directive / RegEx globally ? so that I do not want to go to each page and touch every input by adding ng-pattern or adding directive.

My application has around 200 pages , I don't want to restrict each text input with only alpha numeric.

is there any solution write mon logic that will apply for whole application ? Please suggest, I am using Angular 8.

Thank you In advance.

Share Improve this question asked Oct 1, 2020 at 21:49 Praveen BomminaniPraveen Bomminani 432 silver badges9 bronze badges 3
  • You could apply it to all inputs using a directive but I don't know if you really want to do that. You might be better served by writing your own input ponent or your own form control factory – Aluan Haddad Commented Oct 1, 2020 at 23:35
  • 1 Thank you Aluan, if I create directive .. I need to import that directive in all ponents. basically I don't want to touch any input or ponent.. is there a way I can do by importing that directive in app-module ? the it will effect in all ponents. Please suggest. – Praveen Bomminani Commented Oct 2, 2020 at 12:25
  • 1 I got it, I need to define in root module, as suggested in stackblitz./edit/…. – Praveen Bomminani Commented Oct 2, 2020 at 12:37
Add a ment  | 

1 Answer 1

Reset to default 4

if you make a directive with selector input is applied to all yours inputs

@Directive({
  selector: 'input'
})

You can use a directive like this SO you are next to get it, only replace the input set mask for something like

  @Input()
  set mask(value) {
    if (value == "*")
      this.notApplied = true;
    else
      this.regExpr = new RegExp(value);
  }

declare regExpr as

  private regExpr = new RegExp(/^[A-Z|a-z|0-9]+$/);

and change the host listener checkin if this.notApplied

  @HostListener('input', ['$event'])
  change($event) {
    if (this.notApplied)
      return;
    ...
  }

All your inputs that has no [mask]="what ever" applied the mask by defect

A input with mask="*" will be a "normal" input

See the forked directive

Post a comment

comment list (0)

  1. No comments so far