Automatic hyphens while typing date, in input type date, Angular 5 [duplicate]

一笑奈何 提交于 2021-01-27 10:42:19

问题


How can i get automatic hyphens in input type date, while typing date?

e.g. i want output something like this... If i want to type date 2018-06-18, while typing after 2018, "-" should be automatically shown and append to 2018 and binding on variable on .ts should be 2018-, same after 06, 2018-06-.

I have controlled input via (keypress) function on input..

In .ts file..

  keypress(event: any) {
    const pattern = /[0-9\- ]/
    let inputChar = String.fromCharCode(event.charCode);
    if (event.keyCode != 8 && !pattern.test(inputChar)) {
      event.preventDefault();
    }
  }

In HTML file...

<input maxlength="10" minlength="10" (keypress)="keypress($event)" placeholder="YYYY-MM-DD" name="dateOfBirth" [(ngModel)]="pupil.dateOfBirth" required>

来源:https://stackoverflow.com/questions/50905432/automatic-hyphens-while-typing-date-in-input-type-date-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!