[英]Validate an input only by 7 digits
The user can enter only 7 figures.用户只能输入 7 个数字。
If the user enters more than 7 figures, there is an error message -> "You need 7 digits"如果用户输入超过 7 位数字,则会出现错误信息 ->“您需要 7 位数字”
For example, if the user enters this -> 12345678910
例如,如果用户输入这个 ->
12345678910
I retrieve correctly the error message我正确检索到错误信息
Here is the code这是代码
...
<input type="text" class="inputSignInAccess" [(ngModel)]="otp" >
<div class="error" style="color: red; position: absolute; right: -430px; top: -210px">
<div *ngIf="otp?.length > 7 ">You need 7 digits
</div>
Now, if the user enters 3 figures, I have to retrieve the same error message.现在,如果用户输入 3 个数字,我必须检索相同的错误消息。 My problem is that, I get an error message before to enter a number.
我的问题是,我在输入数字之前收到一条错误消息。 The problem is the value 0 in my condition?
问题是我的情况下值为 0 吗?
<input type="text" class="inputSignInAccess" [(ngModel)]="otp" >
<div class="error" style="color: red; position: absolute; right: -430px; top: -210px">
<div *ngIf="otp?.length > 7 ">
You need 7 digits
</div>
<div *ngIf="otp?.length != 0 ">
</div>
<div *ngIf="otp?.length < 7 ">
You need 7 digits
</div>
</div>
Angular's way to achieve your requirements is to use validators. Angular 实现您的要求的方法是使用验证器。
<input #otpControl="ngModel" type="text" class="inputSignInAccess" [(ngModel)]="otp" minlength="7" maxlength="7" required>
<div *ngIf="otpControl.invalid" class="error" style="color: red; position: absolute; right: -430px; top: -210px">
<div *ngIf="otpControl.errors?.required">Required</div>
<div *ngIf="otpControl.errors?.minlength || otpControl.errors?.maxlength">You need 7 digits</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.