Regex 如何添加遮罩';yyyy/yyyy';在angular2中,使用管道和格式化程序输入字段
我想知道如何使用正则表达式为输入字段创建掩码 我需要向字段添加一些掩码,例如:yyyy/yyy。这听起来像个句号 我看到一个链接,它使用Angular2中的管道创建遮罩。这是你的电话号码 因此,我想用不同的正则表达式创建一个管道,以允许用户只写以下内容:yyyy/yyyy;并利用管道的变换方法。 这可能吗 这是我的管道和格式化程序:Regex 如何添加遮罩';yyyy/yyyy';在angular2中,使用管道和格式化程序输入字段,regex,angular,angular2-forms,angular2-directives,angular2-pipe,Regex,Angular,Angular2 Forms,Angular2 Directives,Angular2 Pipe,我想知道如何使用正则表达式为输入字段创建掩码 我需要向字段添加一些掩码,例如:yyyy/yyy。这听起来像个句号 我看到一个链接,它使用Angular2中的管道创建遮罩。这是你的电话号码 因此,我想用不同的正则表达式创建一个管道,以允许用户只写以下内容:yyyy/yyyy;并利用管道的变换方法。 这可能吗 这是我的管道和格式化程序: import { Pipe, PipeTransform } from "@angular/core"; @Pipe({ name: "mypipe" }) exp
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({ name: "mypipe" })
export class MyPipe implements PipeTransform {
private SEPARATOR: string;
constructor() {
this.SEPARATOR = "/";
}
transform(value): string {
let integer = (value || "").toString();
// Here is where the code should be, to transform the value
return integer;
}
transform(value): string {
// parse method
}
}
import { Directive, HostListener, ElementRef, OnInit } from "@angular/core";
// import { MyPipe } from ...
@Directive({ selector: "[myFormatter]" })
export class MyFormatterDirective implements OnInit {
private el: HTMLInputElement;
constructor(
private elementRef: ElementRef,
private mypipe: MyPipe
) {
this.el = this.elementRef.nativeElement;
}
ngOnInit() {
this.el.value = this.mypipe.transform(this.el.value);
}
@HostListener("keydown", ["$event.target.value"])
handleKeyboardEvent(value) {
this.el.value = this.mypipe.transform(value);
}
}