【发布时间】:2019-06-14 18:49:19
【问题描述】:
我有一个输入字段(输入文本),我必须避免在其中引入任何没有数字的字符。此外,该字段仅限于 4 个数字(一年字段)。 当我得到它时,该字段不允许使用退格键或键盘上的删除键。
我从这个页面尝试了几个解决方案,但没有一个对我有用。
我认为问题与仅限数字的字段限制有关,因为如果我删除此选项,键 backspace 和 delete 工作正常!
.html 是:
<tab heading="{{'Ressources du Foyer' | uppercase}}" [disabled]="!isEdit || !isODP">
<table [defaultElemPerPage]="999" [data]="dataRessources" [config]="configRessources" [columns]="columnsRessources"
[showElementsPerPageSet]="false" [showItemNumberInfo]="false" [doHover]="false" [doClick]="false" [showActionsHeader]="!modeConsultation">
<template let-data>
<p class="text-center" *ngIf=!modeConsultation>
<a (click)="addRowLineRessources(data.index)" class="purple-icon" *ngIf="data.last" title="Ajouter une ligne"><span class="glyphicon glyphicon-plus"></span></a>
<a (click)="removeRowLineRessources(data.index)" title="Supprimer une ligne"><span class="purple-icon glyphicon glyphicon-trash"></span></a>
</p>
</template>
</table>
</tab>
文件.ts:
anneeReference: this.createStandardComponentService.createInputText({
id: 'anneeReference',
type: 'text',
disabled: false,
group: group.get('anneeReference'),
errors: this.errors.anneeReference,
outputMethod: this.validYear,
maxlength: 4
}),
validYear(event: any) {
const pattern = Constants.NUMBERS_PATTERN;
const codeCle = event.keyCode;
const inputChar = String.fromCharCode(event.charCode);
if (!pattern.test(inputChar) && (codeCle !== Constants.BACKSPACE_TOUCHE || codeCle !== Constants.DELETE_TOUCHE)) {
event.preventDefault();
}
}
我希望有一个最多包含 4 个字符的字段,只有数字,我可以随时擦除任何更改。
【问题讨论】:
-
也许您应该尝试重构您的解决方案。阅读更多stackoverflow.com/questions/34556035/…
-
感谢@NicolaeOlariu 的回答。如果你看到代码,我们不使用表格而不是 HTML 代码。这个解决方案对我不起作用。