【发布时间】:2022-11-22 01:18:38
【问题描述】:
我想将输入名称限制为 45 并在超过限制时显示消息。下面我附上了我的 html 文件和 ts 文件。我使用角度 10 。还有其他方法可以限制输入并显示警告消息。谢谢
newUserRegForm = new FormGroup({
'username': new FormControl('', Validators.required , Validators.maxLength(45)),
'password': new FormControl('', Validators.required),
'cpassword': new FormControl('', Validators.required),
'role': new FormControl('Security Engineer', Validators.required),
'projectAccessId': new FormControl([]),
'userEmail': new FormControl('', Validators.email),
});
<form [formGroup]="newUserRegForm">
<mat-form-field class="registerInputForm" fxFlex>
<mat-label>User Name</mat-label>
<input matInput maxlength="45" formControlName="username">
<mat-error *ngIf="newUserRegForm.get('username').touched &&
newUserRegForm.get('username').hasError('required')">
Username is <strong>required</strong>
</mat-error>
<mat-error *ngIf="newUserRegForm.get('username').touched &&
newUserRegForm.get('username').hasError('maxLength')">
maximum length <strong>exceed</strong>
</mat-error>
</mat-form-field>
<br>
<mat-form-field *ngIf="!data?.user" class="registerInputForm">
<mat-label>Password</mat-label>
<input matInput type="password" formControlName="password">
<mat-error *ngIf="newUserRegForm.get('password').touched &&
newUserRegForm.get('password').hasError('required')">
Password is <strong>required</strong>
</mat-error>
</mat-form-field>
</form>
【问题讨论】:
标签: javascript angular forms validation angular-material