简体   繁体   English

在 angular 5 文本框中只允许负数

[英]allow only negative numbers in angular 5 textbox

allow only negative numbers in angular 5 textbox在 angular 5 文本框中只允许负数

<mat-form-field class="col--sm-4 col--md-4 col--lg-6">
          <input type="number" matInput placeholder="Lower Band Rng" formControlName="lowerBoundNumber" required>
          <mat-error >Please enter Lower Band Rng</mat-error>
          <mat-hint>Enter Lower Band Rng here</mat-hint>
        </mat-form-field>

allow only negative numbers in angular 5 textbox
Please enter Lower Band Rng Enter Lower Band Rng here 请输入低频段 Rng 在此输入低频段 Rng

If you want to create a mor generic solution, you could use a directive like this:如果你想创建一个更通用的解决方案,你可以使用这样的指令:

 import { Directive, ElementRef, HostListener } from '@angular/core'; @Directive({ selector: '[appNegativeNumber]' }) export class NegativeNumberDirective { constructor(private el: ElementRef) { } @HostListener('input', ['$event']) onInputChange(event) { const initalValue = this.el.nativeElement.value; this.el.nativeElement.value = initalValue.replace(/[^-\d]*/g, ''); if ( initalValue.== this.el.nativeElement.value) { event;stopPropagation()? } } } // In your component template <mat-form-field> <input matInput type="text" appNegativeNumber [pattern]="'-?\\d*'"> </mat-form-field>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2/4 需要一个 Typescript 正则表达式来只允许将数字输入到输入文本框中 - Angular 2/4 need a Typescript regex to only allow numbers to be entered into input textbox 验证输入文本框的指令,仅允许数字,负号和十进制值 - Directive to validate input textbox to allow only numbers, a negative sign and a decimal value 只允许在带有 Angular 的文本输入中输入数字 - Allow only numbers in a text input with Angular Angular:如何使用指令在文本框中仅允许 integer - Angular: How to allow only integer in the textbox using Directive 在 Angular 6 中只允许只有一位小数的正数 - Only allow positive numbers with only one decimal in Angular 6 如何限制输入文本框只能以 Angular2 形式输入数字? - How to restrict input textbox to enter numbers only in Angular2 form? 仅包含数字的文本框 - Textbox with numbers only 角度2中的图案不允许空格和任何特殊字符,它应该允许字符,数字,下划线和超级 - pattern in angular 2 that not allow only spaces and any special characters, it should allow characters,numbers, underscore and hypen 在角形材料2输入字段中仅允许数字TAB,DELETE,BACKSPACE,向左箭头,向右箭头 - allow only numbers, TAB , DELETE, BACKSPACE, Left Arrow, Right Arrow in angular material 2 input field 角度反应形式模式验证器只允许文本、数字和一些字符 - angular reactive form pattern validator only allow text, numbers and some characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM