Angular 表單驗證

input 雙向綁定 [(ngModel)]="form.username" Ts引入 FormsModule

input 綁定 #username=”ngModel”
input 綁定name name="username"
input 綁定required required
input 綁定minlength minlength="ˇ"
input 綁定pattern pattern="^09\d{8}$"

input (change)="onChangeName(username)"
(change)行為函式onChangeName帶入參數username,
console.log(username)
// touched 觸擊
// invalid 無效
// errors 錯誤 [‘required’] 或 [‘minlength’] 或 [‘pattern’]

如果觸擊並且無效 或是 usernameMessage
*ngIf="username.touched && username.invalid || usernameMessage"

form 綁定#loginForm=”ngForm” 點擊 (ngSubmit)=”loginSubmit(loginForm)
< form #loginForm="ngForm" (ngSubmit)="loginSubmit(loginForm)" >
loginSubmit(loginForm) 函式 獲得參數=> 如果不等於空時,跳出驗證

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  <div class="form_conatiner">
<form #loginForm="ngForm" (ngSubmit)="loginSubmit(loginForm)">
<div class="mb-3 row">
<!--用戶-->
<label for="username" class="col-sm-2 col-form-label">用戶</label>
<div class="col-sm-10">
<input
#username="ngModel"
[(ngModel)]="form.username"
(change)="onChangeName(username)"
[ngClass]="{'error_border':username.touched && username.errors?.['required'] || username.touched && username.errors?.['minlength'] || usernameMessage }"
minlength="3"
required
type="text"
name="username"
class="form-control" id="username" placeholder="用戶">
<small class="text-danger mt-1"
*ngIf="username.touched && username.invalid || usernameMessage">
<div *ngIf="username.errors?.['required']"> 用戶不能為空!</div>
<div *ngIf="username.errors?.['minlength']"> 用戶不能少於3!</div>
</small>
</div>
</div>
<!--身分證字號-->
<div class="mb-3 row">
<label for="identity" class="col-sm-2 col-form-label">身分證字號</label>
<div class="col-sm-10">
<input
#identity="ngModel"
[(ngModel)]="form.identity"
(change)="onChangeIdentity(identity)"
[ngClass]="{'error_border':identity.touched && identity.errors?.['required'] || identity.errors?.['pattern'] || identityMessage }"
name="identity"
pattern="^[A-Za-z][12]\d{8}$"
required
type="text"
class="form-control"
id="identity"
placeholder="身分證字號"
>
<small class="text-danger mt-1"
*ngIf="identity.touched && identity.invalid || identityMessage">
<div *ngIf="identity.errors?.['required']"> 身分證字號不能為空!</div>
<div *ngIf="identity.errors?.['pattern']"> 身分證字號規格不符!</div>
</small>
</div>
</div>
<!--電子郵件-->
<div class="mb-3 row">
<label for="email" class="col-sm-2 col-form-label">電子郵件</label>
<div class="col-sm-10">
<input
#email="ngModel"
[(ngModel)]="form.email"
(change)="onChangeEmail(email)"
[ngClass]="{'error_border':email.touched && email.errors?.['required'] || email.errors?.['pattern'] || emailMessage }"
name="email"
pattern="^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
required
type="text"
class="form-control"
id="email"
placeholder="電子郵件"
>
<small class="text-danger mt-1"
*ngIf="email.touched && email.invalid || emailMessage">
<div *ngIf="email.errors?.['required']">電子郵件不能為空!</div>
<div *ngIf="email.errors?.['pattern']">電子郵件規格不符!</div>
</small>
</div>
</div>
<!--行動電話-->
<div class="mb-3 row">
<label for="phone" class="col-sm-2 col-form-label">行動電話</label>
<div class="col-sm-10">
<input
#phone="ngModel"
[(ngModel)]="form.phone"
(change)="onChangePhone(phone)"
[ngClass]="{'error_border':phone.touched && phone.errors?.['required'] || phone.errors?.['pattern'] || phoneMessage }"
pattern="^09\d{8}$"
name="phone"
type="text"
required
id="phone"
placeholder="行動電話"
class="form-control">
<small class="text-danger mt-1"
*ngIf="phone.touched && phone.invalid || phoneMessage">
<div *ngIf="phone.errors?.['required']"> 行動電話不能為空!</div>
<div *ngIf="phone.errors?.['pattern']"> 行動電話09開頭規格不符!</div>
</small>
</div>
</div>
<!--密碼-->
<div class="mb-3 row">
<label for="password" class="col-sm-2 col-form-label">密碼</label>
<div class="col-sm-10">
<div class="password">
<input
#password="ngModel"
(change)="onChangePassword(password)"
[ngClass]="{'error_border':password.touched && password.errors?.['required'] || password.touched && password.errors?.['minlength'] || passwordMessage }"
type="password"
[type]="isSeePassword ? 'text' : 'password'"
name="password"
minlength="6"
required
ng-model="inputText"
[(ngModel)]="form.password"
class="form-control"
placeholder="{{passwordPlaceholder}}"
id="password">
<i class="fa-solid fa-eye-slash"
[ngClass]="{'fa-eye':isSeePassword}"
(click)="SeePassword()"></i>
</div>
<small class="text-danger mt-1"
*ngIf="password.touched && password.invalid || passwordMessage">
<div *ngIf="password.errors?.['required']"> 密碼不能為空!</div>
<div *ngIf="password.errors?.['minlength']"> 密碼不能少於6!</div>
</small>
</div>
</div>
<!--驗證碼-->
<div class="mb-3 row verificationCode">
<label for="verificationCode" class="col-sm-2 col-form-label">驗證碼</label>
<div class="col-sm-7">
<input
#verificationCode="ngModel"
[(ngModel)]="form.verificationCode"
(change)="onChangeVerificationCode(verificationCode)"
[ngClass]="{'error_border':
verificationCode.touched && verificationCode.errors?.['required'] ||
form.verificationCode!='' &&newVerificationCode!=form.verificationCode || verificationCodeMessage
}"
minlength="4"
name="verificationCode"
type="text"
required
id="verificationCode"
placeholder="驗證碼"
class="form-control">
<small class="text-danger mt-1"
*ngIf="verificationCode.touched && verificationCode.invalid || verificationCodeMessage" >
<div *ngIf="verificationCode.errors?.['required']">驗證碼不能為空!</div>
<div *ngIf="form.verificationCode!='' &&newVerificationCode!=form.verificationCode">{{verificationCodeValue}}</div>
</small>
</div>
<!--驗證碼顯示-->
<div class="col-sm-2 code_box" (click)="showCode()">
<ul>
<li *ngFor="let i of code_box">
{{ i}}
</li>
</ul>
</div>
<!--點擊產生新的驗證碼-->
<div class="col-sm-1">
<i (click)="showCode()" class="fa-solid fa-arrows-rotate"></i>
</div>
</div>
<div class="btn_group">
<button class="btn btn-outline-secondary" type="submit">重置</button>
<button class="btn btn-outline-primary ml-2"
[disabled]="usernameMessage || identityMessage || emailMessage || phoneMessage|| passwordMessage ||verificationCodeMessage"
type="submit">送出</button>
</div>
</form>
</div>

ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { NgClass, NgFor, NgIf } from '@angular/common';
import { Component, inject, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router } from "@angular/router";
import { HttpProviderService } from '../api/http-provider.service';
interface formType{
username: string | undefined,
identity: string | undefined,
email: string | undefined,
phone: string | undefined,
password: string | undefined,
verificationCode: string | undefined,
}
@Component({
selector: 'app-login',
standalone:true,
imports: [FormsModule,NgFor,NgIf,NgClass],
templateUrl: './login.component.html',
styleUrl: './login.component.scss'
})

export class LoginComponent implements OnInit {
private readonly httpProvider = inject(HttpProviderService);
constructor(private router: Router) {
this.generateCode();
}
form:formType={
username: '',
identity:'',
email: '',
phone:'',
password: '',
verificationCode:''
}
// 是否看見密碼
isSeePassword:boolean = false;
SeePassword() {
this.isSeePassword = !this.isSeePassword;
}
passwordPlaceholder:string='請輸入密碼'
onChangeName(x: { value: string | any[]; touched: any; } ) {
x.value.length===0 || x.touched && x.value.length < 3 ? this.usernameMessage = true:this.usernameMessage = false;
}

onChangeIdentity(x: { value: string | any[]; } ) {
x.value.length===0 ? this.identityMessage = true:this.identityMessage = false;
}

onChangeEmail(x: { value: string | any[]; touched: any; }) {
x.value.length===0 || x.touched && x.value.length < 3 ? this.emailMessage = true:this.emailMessage = false;
}

onChangePhone(x: { value: string | any[]; touched: any; }) {
x.value.length===0 || x.touched && x.value.length < 3 ? this.phoneMessage = true:this.phoneMessage = false;
}

onChangePassword(x: { value: string | any[]; touched: any; }) {
x.value.length===0 || x.touched && x.value.length < 6 ? this.passwordMessage = true:this.passwordMessage = false;
}

verificationCodeValue = '';
onChangeVerificationCode(x: any) {
if (x.value.length===0 || x.touched ) {
this.verificationCodeMessage = true;
}
else if (x.value != '') {
if (this.newVerificationCode != x.value) {
this.verificationCodeValue = '驗證碼不正確!';
this.verificationCodeMessage = true;
} else {
this.verificationCodeMessage = false;
}
}
else {
this.verificationCodeMessage = false;
}
}
usernameMessage: boolean = false;
identityMessage: boolean = false;
emailMessage: boolean = false;
phoneMessage: boolean = false;
passwordMessage: boolean = false;
verificationCodeMessage: boolean = false;
// 用戶的表單規則:
userRule(usernameLength:number) {
switch (true) {
case usernameLength ===0 :
this.usernameMessage = true;
break;
case usernameLength <3:
this.usernameMessage = true;
break;
default:
this.usernameMessage = false;
}
}
// Identity的表單規則:正則表達式不能加引號
regIdentityRulePattern: any = /^[A-Za-z][12]\d{8}$/;
identityRule(identity: string) {
switch (true) {
case identity.length ===0 :
this.identityMessage = true;
break;
case !this.regIdentityRulePattern.test(identity):
this.identityMessage = true;
break;
default:
this.identityMessage = false;
}
}
//電子郵件的表單規則:正則表達式不能加引號
regMailRulePattern:any= /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
emailRule(email: string) {
switch (true) {
case email.length ===0 :
this.emailMessage = true;
break;
case !this.regMailRulePattern.test(email):
this.emailMessage = true;
break;
default:
this.emailMessage = false;
}
}
//行動電話的表單規則:正則表達式不能加引號
regPhoneRulePattern:any= /^09\d{8}$/;
phoneRule(phone:string) {
switch (true) {
case phone.length ===0 :
this.phoneMessage = true;
break;
case !this.regPhoneRulePattern.test(phone):
this.phoneMessage = true;
break;
default:
this.phoneMessage = false;
}
}
// 密碼的表單規則
passwordRule(passwordLength:number) {
switch (true) {
case passwordLength ===0 :
this.passwordMessage = true;
break;
case passwordLength <6:
this.passwordMessage = true;
break;
default:
this.passwordMessage = false;
}
}
// 驗證碼的表單規則
verificationCodeRule(verificationCode:string) {
switch (true) {
case verificationCode.length ===0 :
this.verificationCodeMessage = true;
break;
case this.verificationCodeValue==='驗證碼不正確!':
this.passwordMessage = true;
break;
default:
this.verificationCodeMessage = false;
}
}

loginSubmit(loginForm: any) {
this.userRule(loginForm.value.username.length)
this.identityRule(loginForm.value.identity)
this.emailRule(loginForm.value.email)
this.phoneRule(loginForm.value.phone)
this.passwordRule(loginForm.value.password.length)
this.verificationCodeRule(loginForm.value.verificationCode)
if (this.emailMessage || this.phoneMessage || this.passwordMessage ) {
console.log('表單漏填');
console.log(this.emailMessage ,this.phoneMessage,this.passwordMessage );
} else {
console.log('loginForm',loginForm)
let query = {
email: loginForm.email,
password: loginForm.password,
}
this.httpProvider.login(query).subscribe({
next: (res: any) => {
localStorage.setItem('token', res.token);
this.router.navigate(["test_login"]);
}
})
}
}

code_box: object | null | undefined | any = [];
newVerificationCode: string = ''
// 產生認證碼
generateCode(length=4) {
let chars = "0123456789";
let code = "";
for (var i = 0; i < length; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
this.newVerificationCode = code;
//驗證碼分割
this.code_box = code.split('');
}
//點擊獲得新的驗證碼
showCode() {
this.generateCode();
}
ngOnInit(): void {
console.log('typeof this.usernameMessage 判斷型別',typeof this.usernameMessage)
}
}
表單驗證github
參考資料