Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

accept 2 values after decimal in angular forms

import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
  selector: '[appTwoDigitDecimaNumber]'
})
export class TwoDigitDecimaNumberDirective {
  private regex: RegExp = new RegExp(/^d*.?d{0,2}$/g);
  private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', '-', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];
  constructor(private el: ElementRef) {
  }
  @HostListener('keydown', ['$event'])
  onKeyDown(event: KeyboardEvent) {
    console.log(this.el.nativeElement.value);
    // Allow Backspace, tab, end, and home keys
    if (this.specialKeys.indexOf(event.key) !== -1) {
      return;
    }
    let current: string = this.el.nativeElement.value;
    const position = this.el.nativeElement.selectionStart;
    const next: string = [current.slice(0, position), event.key == 'Decimal' ? '.' : event.key, current.slice(position)].join('');
    if (next && !String(next).match(this.regex)) {
      event.preventDefault();
    }
  }
}
Comment

accept 2 values after decimal in angular forms

<input type="textbox" [(ngModel)]="InputValue" appTwoDigitDecimaNumber>
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript get width 
Javascript :: adjust color of text js javascript 
Javascript :: razor list to js array 
Javascript :: simple js drawing program 
Javascript :: expect any function jest 
Javascript :: node convert string to hash 
Javascript :: how to deploy nextjs app on netlify 
Javascript :: javascript write to firebase 
Javascript :: TypeError: JSON.stringify(...).then is not a function 
Javascript :: axios api post request 
Javascript :: new date() javascript 
Javascript :: javascript event currenttarget 
Javascript :: some method in js 
Javascript :: flutter or react native 
Javascript :: odd or even js 
Javascript :: canvas text gradient 
Javascript :: parse json c# 
Javascript :: fibonacci series with recursion in javascript 
Javascript :: json stands for 
Javascript :: odd even javascript 
Javascript :: falsy values in js 
Javascript :: check if variable is set javascript 
Javascript :: trigger jquery 
Javascript :: next js react image upload 
Javascript :: text input placeholder font family react native 
Javascript :: check-if-a-javascript-string-is-a-url 
Javascript :: react native margin vs padding 
Javascript :: how to get keys from request headers in express 
Javascript :: fizzbuzz javascript 
Javascript :: how to write a javascript function 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =