npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ngb-validation

v2.0.0

Published

This custom Angular library provides validators and a directive to help with form validation and input formatting. It includes the following features:

Downloads

59

Readme

Custom Angular Validation and Formatting Library

This custom Angular library provides validators and a directive to help with form validation and input formatting. It includes the following features:

  • Custom Validators for:
    • No Whitespace
    • Password Match
    • URL Format
    • Strong Password
    • File Type & Size Validation
  • Phone Number Formatting directive to apply specific input formats.

Installation

To install the library in your Angular project, you can either:

  1. Download the source files and include them in your project.
  2. Use npm to install it (if you plan to publish it to npm later):
npm install ngb-validation

Using Yarn

Alternatively, you can install it using Yarn:

yarn add ngb-validation

Usage

1. Importing Validators and Directives

Import the required validators and directives into your Angular component or module.

import { noWhitespaceValidator, matchPasswordValidator, urlValidator, strongPasswordValidator, fileValidator, PhoneNumberFormatDirective } from 'ngb-validation';

Then, you can apply these validators in your form group:

import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { noWhitespaceValidator, matchPasswordValidator, urlValidator, strongPasswordValidator, fileValidator } from 'ngb-validation';

@Component({
  selector: 'app-form-example',
  templateUrl: './form-example.component.html',
})
export class FormExampleComponent {
  form: FormGroup;
  formData: FormData = new FormData();

  constructor(private fb: FormBuilder) {
    this.form = this.fb.group({
      username: ['', [noWhitespaceValidator()]],
      password: ['', [strongPasswordValidator()]],
      confirmPassword: ['', [matchPasswordValidator('password', 'confirmPassword')]],
      website: ['', [urlValidator()]],
      profileImage: [{}, [fileValidator(['image/jpeg', 'image/png'], 5000000)]]
    });
  }

  onSubmit() {
    if (this.form.valid) {
      console.log('Form Submitted!', this.form.value);
    } else {
      console.log('Form Invalid!');
    }
  }

  onFileSelected(event: Event): void {
    const input = event.target as HTMLInputElement;

    if (input?.files?.length) {
      const file = input.files[0];
      let control: any = this.form.get('profileImage');
      this.form.get('profileImage')?.updateValueAndValidity();
      control?.setValue(file);
      if (control?.valid) {
        console.log('File is valid:', file);
        // Append the file to FormData
        this.formData.set('profileImage', file, file.name);
      }
    }
    this.form.get('profileImage')?.markAsTouched();
  }
}

2. Phone Number Format Directive

To use the phone number format directive, first import it:

import { PhoneNumberFormatDirective } from 'ngb-validation';
@Component({
  selector: 'app-form-example',
  templateUrl: './form-example.component.html',
  imports: [PhoneNumberFormatDirective]
  });
  export class FormExampleComponent {
    @Input() format: string = '000-000-0000';
    ....
    ....
}

Then, add the directive to your component:

<input type="text" appPhoneNumberFormat [format]="format" [(ngModel)]="phoneNumber" />

This will automatically format the phone number input as the user types. You can also customize the format by changing the format input.

3. Example HTML Template

Here's an example HTML template for using the other validators and phone number format directive:

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <label for="username">Username:</label>
  <input id="username" formControlName="username" type="text" />
  <div *ngIf="form.get('username')?.hasError('whitespace')">
    Username cannot contain only spaces.
  </div>

  <label for="password">Password:</label>
  <input id="password" formControlName="password" type="password" />
  <div *ngIf="form.get('password')?.hasError('weakPassword')">
    Password must contain at least 8 characters, including an uppercase letter, a number, and a special character.
  </div>

  <label for="confirmPassword">Confirm Password:</label>
  <input id="confirmPassword" formControlName="confirmPassword" type="password" />
  <div *ngIf="form.get('confirmPassword')?.hasError('passwordMismatch')">
    Passwords do not match.
  </div>

  <label for="website">Website URL:</label>
  <input id="website" formControlName="website" type="text" />
  <div *ngIf="form.get('website')?.hasError('invalidUrl')">
    Please enter a valid URL.
  </div>

  <label for="profileImage">Profile Image (JPEG/PNG):</label>
  <input id="profileImage" formControlName="profileImage" type="file" class="form-control" (change)="onFileSelected($event)" [ngClass]="{'is-invalid': form.get('profileImage')?.invalid && form.get('profileImage')?.touched}" />
    <div *ngIf="form.get('profileImage')?.touched && form.get('profileImage')?.hasError('invalidFile')" class="invalid-feedback">
      Please upload a valid file type (JPEG/PNG) and ensure the file size is less than 5MB.
    </div>

  <label for="phoneNumber">Phone Number:</label>
  <input id="phoneNumber" appPhoneNumberFormat [format]="format" formControlName="phoneNumber" type="text" />
  <div *ngIf="form.get('phoneNumber')?.hasError('invalidPhoneNumber')">
    Please enter a valid phone number.
  </div>

  <button type="submit" [disabled]="form.invalid">Submit</button>
</form>

Issues

If you encounter any issues or bugs, please report them at: GitHub Issues


Contributing

We welcome contributions! Feel free to submit a pull request or open an issue to discuss any changes.


Conclusion

This library provides essential form validation and input formatting tools for Angular projects. You can easily integrate these custom validators and directives into your forms, providing a better user experience and more consistent validation across your application.