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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-input-msg

v1.0.2

Published

Provides form input live validation for Angular 5+ projects

Downloads

26

Readme

ngx-input-msg

Provides form input live validation for Angular 5+ projects. Demo

This project is inspired by AngularJS ng-messages module.

Features

  • Integrated with Angular template driven forms.

  • Minifies boilerplate code, easy to add error messages to form inputs.

  • Supports HTML5 like validation syntax (required, min, max, minlength, maxlength, pattern). It is also provides email and integer validators.

  • Flexible. You can configure error messages once via InputMsgConfigService and provide them to the entire application or set the specific message to the specific input.

  • Compatible with Angular Material Design form-field element as well as basic HTML input.

Get started

  1. Install Module to your Angular project via: npm install ngx-input-msg --save.

  2. Import InputMsgModule to your feature module.

import { NgModule } from '@angular/core';
import { InputMsgModule } from 'ngx-input-msg';

@NgModule({
  imports: [
    InputMsgModule
  ],
  declarations: [],
  providers: []
})
export class FeatureModule { }
  1. Provide Angular Material to the app, if you are going to use Material style inputs.

  2. Inside a <form>, add ngxInputText directive to an <input> element, then append <ngx-msg> component to show error messages. See Demo

API Reference

AbstractInput

This base class describes common properties and methods for all input directives. Each input directive extends AbstractInput to provide its specific validation parameters and behaviors.

@Input() properties

Name | Required | Reacts on changes* | Description --- | --- | --- | --- id: string | Optional | no | Used to connect an input element with <label> element. label: string | Optional | yes | An input label text. model: NgModel | Required | no | An input NgModel instance. name: string | Required | no | An input name attribute. placeholder: string | Optional | yes | An alias for label property. Used to be shown inside an error message. required: boolean | Optional | yes | Checks if an input value is not empty.

* Whether an @Input argument could be changed after the directive has been initialized.

AbstractInput also toggles ngx-input_invalid CSS class when an input status changes.

InputEmailDirective extends AbstractInput

Directive to validate an <input> or <input type="email"> element as an email address.

Selector: [ngxInputEmail]

InputNumberDirective extends AbstractInput

Directive to validate an <input type="number"> element.

Selector: [ngxInputNumber][type="number"]

Optional @Input() properties

Name | Reacts on changes | Description --- | --- | --- integer: boolean | yes | Checks if an input value is an integer. max: number | string | yes | Checks if an input value does not exceed max value. min: number | string | yes | Checks if an input value is not less than min value.

InputTextDirective extends AbstractInput

Directive to validate text like <input> or <textarea> elements.

Selector: input[ngxInputText], textarea[ngxInputText]

Optional @Input() properties

Name | Reacts on changes | Description --- | --- | --- maxlength: number | string | yes | Checks if an input value length does't exceed maxlength value. minlength: number | string | yes | Checks if an input value length is more than minlength value. pattern: RegExp | yes | Checks if an input value matches with pattern.

LabelDirective

Highlights <label> element when a bound input is invalid. It is also toggles ngx-input_invalid CSS class when an input status changes.

Selector: [ngxLabel]

Required @Input() properties

Name | Reacts on changes | Description --- | --- | --- for: string | no | An input name or id value. Used to connect <label> element with an input element.

MsgComponent

Displays a message for an input element depending on it`s validation status. Only one message can be shown at a time.

Selector: ngx-msg

@Input() properties

Name | Required | Reacts on changes | Description --- | --- | --- | --- for: string | Required | no | An input name or id value. Used to connect <ngx-msg> component with an input element. position: 'bottom-left' | 'bottom-right' | Optional | yes | The position to show the message. Default 'bottom-left'. email: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. integer: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. max: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. maxlength: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. min: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. minlength: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. pattern: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter. required: string | MsgFn* | Optional | yes | An error message for an appropriate validation parameter.

* A function that returns an error message dynamically, depending on label (or placeholder) value.

type MsgFn = (label: string, validationParamValue?: any) => string;

InputMsgConfigService

Provides configuration for displaying messages.

Methods

  interface InputMsgConfigService {
    public get(): Config;
    public set(config: Config): void; 
  }

  
  interface Config {
    colors?: {
      // color to highlight <ngx-msg> and <label> elements when an input is invalid
      error?: string;
      // color to highlight <ngx-msg> element when max length was reached
      maxlength?: string;
    };
    // Position to show a message
    position?: 'bottom-left' | 'bottom-right';
    // Message texts
    msg?: {
      [validatorName: string]: string | MsgFn;
    };
  }

Tip: set default messages by set() method to provide them to all <ngx-msg> components of the application. If a specific message is not provided to <ngx-msg> component, a default message is used instead. You could also use default messages from the box. See the default config below.

Default Config

  {
    colors: {
      error: '#f44336',
      maxlength: 'grey'
    },
    position: 'bottom-left',
    msg: {
      email: (label: string) => `Wrong ${label}`,
      integer: 'Fractional digits are forbidden',
      max: (label: string, allowed: number) => `Maximum allowed ${label} is ${allowed}`,
      min: (label: string, allowed: number) => `Minimum allowed ${label} is ${allowed}`,
      maxlength: (label: string, allowed: number) => `Maximum ${allowed} chars limit was reached`,
      minlength: (label: string, allowed: number) => `At least ${allowed} chars length are required`,
      pattern: (label: string) => `Invalid ${label}`,
      required: (label: string) => `${label} is required`
    }
  };