Html 限制8中浮点指令的浮点数范围

Html 限制8中浮点指令的浮点数范围,html,angular,Html,Angular,我已经在angular 8中创建了自定义指令,并且只允许浮点数达到 小数点2,介于0.00到100.00之间 import { Directive , ElementRef, HostListener} from '@angular/core'; @Directive({ selector: '[appGst]' }) export class GstDirective { private regex: RegExp = new RegExp(/^\d*\.?\d{0,2}$/g);

我已经在angular 8中创建了自定义指令,并且只允许浮点数达到 小数点2,介于0.00到100.00之间

import { Directive , ElementRef, HostListener} from '@angular/core';

@Directive({
  selector: '[appGst]'
})
export class GstDirective {

  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)) {
      console.log(current);
      event.preventDefault();
    }
    
  }

}
从'@angular/core'导入{Directive,ElementRef,HostListener};
@指示({
选择器:“[appGst]”
})
出口类GST指令{
私有regex:RegExp=newregexp(/^\d*\.?\d{0,2}$/g);
专用专用键:数组=[“退格”、“制表符”、“结束”、“主页”、““-”、“箭头左”、“箭头右”、“删除”、“删除”];
构造函数(专用el:ElementRef){
}
@HostListener('keydown',['$event']))
onKeyDown(事件:KeyboardEvent){
log(this.el.nativeElement.value);
//允许使用退格键、制表符键、结束键和主控键
if(this.specialKeys.indexOf(event.key)!=-1){
返回;
}
让当前值:string=this.el.nativeElement.value;
const position=this.el.nativeElement.selectionStart;
const next:string=[current.slice(0,位置),event.key=='Decimal'?'。:event.key,current.slice(位置)].join(“”);
if(next&&!String(next).match(this.regex)){
console.log(当前);
event.preventDefault();
}
}
}

输入的数字现在正在格式化为0.00,但我想更改代码,以便用户只在0.00到100.00之间输入数字。

regExpr必须是
^([0-9](\.\d{0,2})\.[1-9][0-9](\.\d{0,2})\.\100(\.0{0,2})$
。注意,如果您想查看,请考虑使用
^([0-9](\.\d{0,2})\.[1241][0-9](\\.\\d\{0,2\})100(\\.0{0,2\})$
它只允许整数,但我希望是介于0.00到100.00之间的浮点值。是否检查regExpr?在中,您可以检查所有需要的regExpr