你好這裏是工作郵件模式和電子郵件驗證與登錄表單我在我的應用程序已經使用
在HTML
<form [formGroup]="myForm" (ngSubmit)="submitForm(myForm.value)">
<div class="form-group" [ngClass]="{'has-error':!signinForm.controls['usermail'].valid && signinForm.controls['usermail'].touched}">
<label>Email Address</label>
<input class="form-control" type="email" [formControl]="signinForm.controls['usermail']">
<span *ngIf="signinForm.controls['usermail'].hasError('required') && signinForm.controls['usermail'].touched && !signinForm.submitted" class="required pull-right">*Please Enter Email</span>
<span *ngIf="signinForm.controls['usermail'].hasError('pattern') && signinForm.controls['usermail'].touched" class="required pull-right">*Invalid Email</span>
</div>
<div class="form-group" [ngClass]="{'has-error':!signinForm.controls['userpass'].valid && signinForm.controls['userpass'].touched}">
<label>Password</label>
<input class="form-control" type="password" [formControl]="signinForm.controls['userpass']" (focus)="showErrorMessage=false">
<span *ngIf="signinForm.controls['userpass'].hasError('required') && signinForm.controls['userpass'].touched && !signinForm.submitted" class="required pull-right">*Please Enter Password</span>
</div>
<button type="submit" class="btn btn-pri mds_btn" [disabled]="!signinForm.valid">Login</button>
</form>
在組件
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: [ './login.component.scss' ]
})
export class LoginComponent {
myForm: FormGroup;
emailRegex: any = '^[a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,15})$';
passRegex:any ='^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$';
constructor(fb: FormBuilder) {
this.myForm = fb.group({
'logmail': [ null, Validators.compose([ Validators.required, Validators.pattern(this.emailRegex) ]) ],
'logpass': [ null, Validators.compose([ Validators.required, Validators.pattern(this.passRegex) ]) ]
});
this.myForm.valueChanges.subscribe((form: any) => {
});
}
submitForm(lgvalue: any) {
//do logic here after submitting the form
}
}
在這個例子中,我已經使用emailRegex這是有效的電子郵件模式 如果你想你可以在這裏開發自己的模式http://www.html5pattern.com/
其工作謝謝! –
@SelvaKumarDuraisamy如果這個答案幫助你請upvote或給綠色印記。 :) – Malhari
對不起,我有不到15名的聲譽 –