如何在模板方法中限制输入字段仅接受内部字符串? [英] How can i restrict input field to accept just string inside in template approach?
本文介绍了如何在模板方法中限制输入字段仅接受内部字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表单中有输入字段.不应允许用户在其中输入数字.只能是字符串.这是我在模板方法中的输入,在此方法中,我限制长度必须至少为3个字符,并且是必需的.
I have input field in form.The user should not be allowed to write numbers inside.Just strings. This is my input in template approach where i am restricting that the length should be minimum 3 characters and is required.
<input class="form-control"
id="productNameId"
type="text"
placeholder="Name (required)"
required
minlength="3"
[(ngModel)]=product.productName
name="productName"
#productNameVar="ngModel"
[ngClass]="{'is-invalid': (productNameVar.touched ||
productNameVar.dirty ||
product.id !== 0) &&
!productNameVar.valid }" />
<span class="invalid-feedback">
<span *ngIf="productNameVar.errors?.required">
Product name is required.
</span>
<span *ngIf="productNameVar.errors?.minlength">
Product name must be at least three characters.
</span>
</span>
推荐答案
在输入标签上,使用pattern属性将正则表达式用于验证:
On your input-tag use the pattern attribute to use regex for your validation:
<input ... pattern="[A-Za-z]{3,}" required />
这篇关于如何在模板方法中限制输入字段仅接受内部字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文