如何验证Ionic 3中textarea的最小长度? Maxlength工作得很好但很奇怪minLength不起作用。
<ion-textarea id="emailBody" minlength="2" rows="3" required maxLength="500" type="text" [(ngModel)]="emailBody"></ion-textarea>
答案 0 :(得分:2)
你可以尝试这个解决方案
ts文件代码
formObj:any={
emailBody:""
}
html文件代码
<ion-textarea minlength="2" rows="3" required maxLength="500" type="text" [(ngModel)]="formObj.emailBody" #emailBody="ngModel"></ion-textarea>
<div *ngIf="emailBody.invalid && (emailBody.dirty || emailBody.touched)"
class="alert alert-danger">
<div *ngIf="emailBody.errors.required">
emailBody is required.
</div>
<div *ngIf="emailBody.errors.minlength">
emailBody must be at least 2 characters long.
</div>
</div>
答案 1 :(得分:0)
我使用了一些Krishna Rathore和我发现的一些东西。
感谢正确的推动。
<div class="padding-left-right-10" *ngIf="!passedEmailTextCheck">
email text is required.
</div>
ts文件
if (!passedEmailText) {
this.passedEmailTextCheck = false;
} else {
// javascript值检查
if( value ) {
}
will evaluate to true if value is not:
null
undefined
NaN
empty string ''
0
false
typescript includes javascript rules.
答案 2 :(得分:-1)
捕获控件上的输入事件并查看长度
<ion-textarea id="emailBody" minlength="2" rows="3" required maxLength="500" type="text" [(ngModel)]="emailBody" (input)="checkLength($event.target.value)"></ion-textarea>
<强> app.component.ts 强>
checkLength(value:any){
if(value.length<=2){
//min length found.
}
};