本文介绍了使用angular2形成maxlength字段验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用angular2 验证带有maxlength或minlength的用户名字段,而不使用formbuilder 。
How to validate the username field with maxlength or minlength using angular2 without using the formbuilder.
I尝试以下方式,但没有工作
I tried in the following way, but not working
<ion-item>
<ion-icon name="person" item-left class="placeholder-icon"></ion-icon>
<ion-label floating primary>Username</ion-label>
<ion-input [(ngModel)]="login.username" ngControl="username" type="text" #username="ngForm" ng-minlength="5" required>
</ion-input>
</ion-item>
<div [hidden]="username.valid || username.pristine">
<p *ngIf="username.errors && username.errors.required" danger padding-left>
user name is required
</p>
<p *ngIf="username.errors && username.errors.minlength" danger padding-left>
min length atleast 5
</p>
</div>
<ion-item>
推荐答案
只需添加 minlength
到你的输入元素。这样, MinLengthValidator
指令将自动添加并注册为验证器:
Simply add the minlength
to your input element. This way the MinLengthValidator
directive will be automatically added and registered as a validator:
<ion-input [(ngModel)]="login.username"
ngControl="username" type="text"
#username="ngForm" minlength="5" required>
这篇关于使用angular2形成maxlength字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!