首页 文章

输入类型仅允许数字和maxLength

提问于
浏览
0

我有一个输入类型,我希望它只允许使用离子3的maxLenght限制数字 .

我试过这个:

<input type="tel" maxlength="5" class="correos-custom-input {{isEmpty}}" [disabled]="isPCVerified" [(ngModel)]="postalCode">

这允许我用maxLenght写数字和字母 .

<input type="number" maxlength="5" class="correos-custom-input {{isEmpty}}" [disabled]="isPCVerified" [(ngModel)]="postalCode">

这只是数字,但具有无限长度,因为maxlenght仅支持类型文本和电话 .

谢谢你的帮助 .

2 回答

  • 1

    您可以使用Angular Form Validators . 对于您的号码问题,您只需将其更改为文本,然后在模块中,在保存之前转换为数字 .

    另一个选项是创建自己的验证器并从当前的数字验证器扩展它并添加自己的长度检查 . 我发现这个article是电子邮件的样本,但可以很容易地修改它来添加长度检查 .

    创建自己的另一个好例子custom validator

  • 1

    试试这个

    <input type="tel" maxlength="5" inputmode="numeric" pattern="^[0-9]*$" class="correos-custom-input {{isEmpty}}" [disabled]="isPCVerified" [(ngModel)]="postalCode">
    

    inputmode 将告诉输入显示设备上的数字键盘, pattern 是预期条目的正则表达式,在这种情况下只是数字 .

    希望这可以帮助 .

相关问题