• Stars
    star
    161
  • Rank 233,470 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An Angular directive to only allow [0-9] in the input box when typing, pasting or drag/dropping.

Angular DigitOnly Directive and Mask Directive

Build Status npm

Demo

Medium Article: Digit Only Directive in Angular

  • input digitOnly directive

    An Angular directive only allows [0-9] in the input box when typing, pasting or drag/dropping. This directive handles both Windows keyboard and Mac keyboard. This directive works with input type="text", not input type="number".

  • input mask directive

    This directive checks the input pattern attribute if set.

CHANGELOG

  • v3.2.0(v2.4.0): digitOnly directive now supports disabling paste events (merges a pull request #57, fixes #56).

  • v3.1.0(v2.3.0): digitOnly directive now supports negative values (merges a pull request #49).

  • v3.0.0: a release on par with Angular 12. For projects in Angular v10 or v11, please use v2 of this library.

  • v2.2.3: fix an issue (#50) in the mask directive: support dynamic pattern attribute binding.

  • v2.2.2: fix an issue (#28) to prevent dead keys in the digitOnly directive.

  • v2.2.1: digitOnly directive now dispatches an input event when paste in Firefox.

  • v2.2.0: fix an issue (#35): for better international support, both mask and digitOnly directives now also check the code attribute in KeyboardEvent.

  • v2.1.0(v1.9.0): fix an issue (#39) when typing decimal numbers for the digitOnly directive

  • v2.0.0: add tslib v2.0 in the dependency, which is required by TypeScript 3.9 (as of Angular 10).

  • v1.8.0: fix an issue (#38) when pasting in IE and Edge for the digitOnly directive

  • v1.7.0: the digitOnly directive allows model binding to min, max, and pattern properties.

    • See demo page for examples.
  • v1.6.0: the mask directive is added to this library.

    • See an example below about an input allows ##-####.
  • v1.5.0: this directive checks the input pattern attribute if set.

    • See an example below about an input only allows decimal numbers with precision of 2.
  • v1.3.0: this directive accepts an attribute for the separator for decimal numbers.

    • By default, the separator is a .. You can set it to comma when needed.
  • v1.1.0: this directive accepts an attribute which indicates if the input number allows a decimal point.


Installation

npm i @uiowa/digit-only

Usage

// in your Angular module
import { DigitOnlyModule } from '@uiowa/digit-only';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    BrowserModule,
    DigitOnlyModule
  ],
  ...
})
export class YourModule { }
// in your component.html
<input type="text" digitOnly />

// pull out the numeric keypad in mobile devices and tablets
<input
  type="text"
  name="zipcode"
  id="zipcode"
  placeholder="00000"
  maxlength="5"
  inputmode="numeric"
  pattern="[0-9]*"
  digitOnly
/>

// turn off browser autocomplete
<input ... autocomplete="off" />

// allows decimal input
<input
  id="decimal-number"
  type="text"
  digitOnly
  decimal="true"
  placeholder="000"
/>

// allows to set decimal separator
<label for="digit-only-decimal-comma">
  Digit Only input box that allows a <i>decimal point</i> using
  <strong>a comma as the separator</strong>
</label>
<input
  id="digit-only-decimal-comma"
  type="text"
  digitOnly
  decimal="true"
  decimalSeparator=","
  placeholder="0,00"
  pattern="[0-9]+([,][0-9]+)?"
/>

// Digit Only input only allows two decimal places
<input
  id="currency"
  type="text"
  name="currency"
  inputmode="numeric"
  pattern="^\d+(\.\d{1,2})?$"
  placeholder="0.00"
  digitOnly
  decimal="true"
/>

mask directive usage

// input masked with pattern attribute: <code>##-####</code>
<input
  id="org-dept"
  type="text"
  pattern="^(\d{0,2}|\d{2}-\d{0,4})$"
  name="org-dept"
  title="org-dept"
  placeholder="00-0000"
  mask
/>

More Repositories

1

angular-material-admin

Angular Material Admin Dashboard
TypeScript
156
star
2

dotnetlabs

.NET Labs -- Show Me the Tips and Tricks and Code
C#
147
star
3

rabbitmq-labs

The source code for my RabbitMQ tutorials.
C#
68
star
4

canvas-animation-demos

Canvas Animations in Typescript
TypeScript
50
star
5

sentiment-analysis-using-python

Large Data Analysis Course Project
Python
26
star
6

canvas-animation-step-by-step

Canvas animation with flowers step by step using TypeScript.
TypeScript
26
star
7

angular-quill-editor

A light weight Angular wrapper of Quilljs, powerful rich text editor
TypeScript
21
star
8

LSTM-sentiment-analysis

LSTM sentiment analysis. Please look at my another repo for SVM and Naive algorithem
Python
20
star
9

session-expiration-alert

Angular module for session expiration alert
TypeScript
18
star
10

date-range-picker

Angular components for Date Range Picker and Date Picker using ng-bootstrap
TypeScript
14
star
11

angular-maze

A maze generator and path finder in Angular
TypeScript
13
star
12

retaining-filters-in-ag-grid

retain filters in ag-Grid when navigating among pages
TypeScript
12
star
13

weather-cli-nodejs

A CLI program in Node.js. The program checks weather data in command line.
JavaScript
11
star
14

historical-weather-data

Historical Weather Data Collection using GitHub Actions
11
star
15

EFCoreDemo

EntityFrameworkCore Demo in a WebAPI project
C#
10
star
16

angular-nginx-docker

Serve a SPA using NGINX in a docker container
TypeScript
6
star
17

uiowa-header-demo

An Angular library for common website header, including a UIOWA branding bar and a website nav bar.
TypeScript
6
star
18

angular-clipboard

Copy a text string from an input element. The solution is not limited to Angular.
TypeScript
6
star
19

spinner

Angular components for spinners
TypeScript
4
star
20

rxjs-spinner-min-duration

Show spinner with minimum duration using rxjs combineLatest operator
TypeScript
4
star
21

css-collection

A collection of CSS card designs
HTML
3
star
22

ScriptsArchive

Useful scripts
PowerShell
3
star
23

MBA-Course-Notes

Notes for my MBA courses
3
star
24

uiowa-mfk-project

Angular module for UIowa MFK input, favorite MFK, MFK string, etc...
TypeScript
2
star
25

Optimization_of_Tensegrity_Structures

Course Project for Optimization Techniques
MATLAB
2
star
26

testing-login-form-with-cypress

End-to-end testing a login form using Cypress
JavaScript
2
star
27

gitmoji

emoji for git commit message. based on https://gitmoji.carloscuesta.me/
2
star
28

DelimitedDataFileHelper

Reader and Writer for Delimited Data File (CSV, tab/pipe delimited file, etc...)
C#
2
star
29

rxjs-playground

A playground project for RxJS 6 in Typescript build using webpack 4
TypeScript
2
star
30

connect4

Angular connect4 game | AI | minimax | turn-based
TypeScript
1
star
31

ArgonMolecularDynamics

course project for HPPC
C
1
star
32

PdfExamples

An Evaluation on PDF libraries in .NET Core
C#
1
star
33

tic-tac-toe

A simple game made with Angular
TypeScript
1
star
34

typescript-starter

A starter project template for TypeScript and HTML using webpack.
TypeScript
1
star
35

CS3980-2024

Python
1
star
36

prevent-from-leaving

Angular prevent from leaving page
TypeScript
1
star
37

typescript-decorator

An introduction to decorator in Typescript; A resemblance to Angular component decorator.
TypeScript
1
star