【发布时间】:2018-12-13 08:26:49
【问题描述】:
我有提交电子邮件的表单,我想添加验证,因此不能是空的(必需),不能是无效的电子邮件,例如 juventusSignedCr7@4.4, 等,但是当我添加电子邮件时,例如 neymarPleaseStopeDiving 到我的输入并单击提交不返回错误并提交数据,只有当我提交空输入时,我才会收到错误消息。电子邮件是必需的
这是我所做的:
更新
组件.ts
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
...............
export class AboutComponent implements OnInit {
angForm: FormGroup;
constructor(private flashMessages: FlashMessagesService,
private fb: FormBuilder) {
this.createForm();
}
createForm() {
this.angForm = this.fb.group({
email: ['', Validators.required]
});
}
HTML
<form [formGroup]="angForm" novalidate class="form-element">
<div class="form-group form-element_email">
<input type="email" class="form-control" name="email" formControlName="email" #email />
</div>
<div *ngIf="angForm.controls['email'].invalid && (angForm.controls['email'].dirty || angForm.controls['email'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['email'].errors.required">
Email is required.
</div>
<div class="form-group">
<button (click)="addReview(email.value)" [disabled]="angForm.pristine || angForm.invalid" class="btn btn-primary form-element_btn">Book</button>
</div>
</form>
问题
我的代码有什么问题?请在这里帮助新手,谢谢
【问题讨论】:
标签: angular html bootstrap-4