How to develop a directive that restricts user input based on custom conditions

0 votes
Can you tell me How to develop a directive that restricts user input based on custom conditions?
4 days ago in Node-js by Nidhi
• 14,600 points
31 views

1 answer to this question.

0 votes

To create an Angular directive that restricts user input according to custom conditions, follow this comprehensive approach:

Basic Directive Structure

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

@Directive({

  selector: '[appInputRestrictor]'

})

export class InputRestrictorDirective {

  @Input() appInputRestrictor: string | RegExp; // Can be regex pattern or validation type

  constructor(private el: ElementRef) {}

  @HostListener('input', ['$event']) onInputChange(event: Event) {

    const input = event.target as HTMLInputElement;

    const initialValue = input.value;

    const regex = this.getValidationPattern();

    if (regex) {

      input.value = initialValue.replace(regex, '');

      if (initialValue !== input.value) {

        event.stopPropagation();

      }

    }

  }

  private getValidationPattern(): RegExp | null {

    if (typeof this.appInputRestrictor === 'string') {

      return this.getPredefinedPattern(this.appInputRestrictor);

    }

    return this.appInputRestrictor;

  }

  private getPredefinedPattern(type: string): RegExp | null {

    const patterns = {

      'numbers-only': /[^0-9]/g,

      'letters-only': /[^a-zA-Z]/g,

      'alphanumeric': /[^a-zA-Z0-9]/g,

      'no-special-chars': /[^\w\s]/g

    };

    return patterns[type] || null;

  }

}

answered 3 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

run npm run script1 it works for me READ MORE

answered Jan 10, 2023 in Node-js by Harisudarsan

edited Mar 5 42,043 views
0 votes
0 answers

How to install NodeJS LTS on Windows as a local user (without admin rights)

I'm using Windows as a simple user ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,020 points
2,123 views
0 votes
0 answers

How to create a responsive navbar that collapses on smaller screens in Bootstrap 3?

With the help of proper code example ...READ MORE

Apr 1 in Node-js by Nidhi
• 14,600 points
37 views
0 votes
1 answer

How to handle the swiperight event to trigger custom actions in jQuery Mobile?

To handle the swiperight event and trigger ...READ MORE

answered 3 days ago in Node-js by anonymous
30 views
0 votes
1 answer

How to implement a directive that auto-saves form data periodically?

To create a directive that automatically saves ...READ MORE

answered 3 days ago in Node-js by anonymous
28 views
0 votes
1 answer

How to dynamically inject components into the DOM using @Component?

You need to use ComponentFactoryResolver and ViewContainerRef. ...READ MORE

answered 3 days ago in Node-js by anonymous
40 views
0 votes
1 answer

How to use host bindings to bind properties to the host element in a component?

In Angular (2+), the @HostBinding() decorator allows ...READ MORE

answered 3 days ago in Node-js by anonymous
40 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP