Angular4表單驗證代碼詳解
背景:
最近在itoo頁面調(diào)整的時候,發(fā)現(xiàn)頁面表單或者是文本框沒有做基本的判斷操作,所以著手demo一篇,希望對大家有幫助??!
--------------------------------------------------------------------------------
1.創(chuàng)建表單組件:
ng g c login1
2.1單規(guī)則驗證:
<label>用戶名:</label>
<input type="text" #userNameRef=ngModel [(ngModel)]=userName required>
<span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.valid}}</span>
--------------------------------------------------------------------------------
效果:


2.2.多規(guī)則驗證:(不能為空,用戶名和密碼的長度)
<div class="form-group">
<label>用戶名:</label>
<input type="text" class="form-control" #userNameRef=ngModel minlength="3" maxlength="8" [(ngModel)]=userName required>
<span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.valid}}</span>
</div>
錯誤原因提示方式:
<div class="form-group">
<label>用戶名:</label>
<input type="text" class="form-control" #userNameRef=ngModel minlength="3" maxlength="8" [(ngModel)]=userName required>
<span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.errors|json}}</span>
<div *ngIf="userNameRef.errors?.required">this is required</div>
<div *ngIf="userNameRef.errors?.minlength">should be 3 chacaters</div>
</div>
效果:
###:初始化,沒有輸入數(shù)據(jù):

###:輸入數(shù)據(jù),但是長度小于3:

###:合法輸入:
當然這里有一個問題,就是合法的時候usernameRef.errors=null,但是用戶看起來不太美觀,所以就需要判斷當usernameRef.errors=null的時不出現(xiàn):
<span [style.color]="userNameRef.valid ? 'black':'red'" *ngIf="userNameRef.errors!=null">{{userNameRef.errors|json}}</span>
具體實例登陸代碼:
<form #form="ngForm" (ngSubmit)="form.form.valid && submit(form.value)" novalidate class="form-horizontal" role="form">
<div class="form-group" [ngClass]="{ 'has-error': form.submitted && !userName.valid }">
<label class="col-sm-2 control-label">用戶名:</label>
<div class="col-sm-10">
<input required name="userName" [(ngModel)]="user.userName" #userName="ngModel" type="text" class="form-control" placeholder="請輸入用戶名...">
<div *ngIf="form.submitted && !userName.valid" class="text-danger">用戶名必須輸入!</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">密碼:</label>
<div class="col-sm-10" [ngClass]="{'has-error': form.submitted && !password.valid }">
<input required minlength="8" maxlength="12" [(ngModel)]="user.password" name="password" #password="ngModel" type="password" class="form-control" placeholder="請輸入密碼...">
<div *ngIf="form.submitted && !password.valid" class="text-danger">密碼必須輸入,至少要8位!</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">登錄</button>
</div>
</div>
</form>
login.component:
import { Component, OnInit} from '@angular/core';
import{UserModel} from '../model/user.model';//引入了usermodel
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor() { }
//定義user為Usermodel
private user=new UserModel();
ngOnInit() {
}
/**
* 登陸事件
* @param form 表單中的輸入值
*/
submit(form){
if(form.username=="1"&&form.password=="12345678"){
alert("登錄成功了");
}else{
alert("非法用戶");
}
}
}
3.userModel:
export class UserModel{
userName:string;
password:string;
}
效果:
1.未填時點擊登陸:
2.輸入登陸:
3.非法用戶:

總結(jié)
以上所述是小編給大家介紹的Angular4表單驗證代碼詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
理解AngularJs篇:30分鐘快速掌握AngularJs
這篇文章主要介紹了理解AngularJs篇:30分鐘快速掌握AngularJs,詳細介紹了AngularJs所涉及的知識點,有興趣的可以了解一下。2016-12-12
Angularjs 雙向綁定時字符串的轉(zhuǎn)換成數(shù)字類型的問題
這篇文章主要介紹了Angular js雙向綁定時字符串的轉(zhuǎn)換成數(shù)字類型的問題,需要的朋友可以參考下2017-06-06
詳解什么是@ngrx/store開發(fā)包中的MemoizedSelector
這篇文章主要為大家介紹了@ngrx/store開發(fā)包中的MemoizedSelector使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
angular.js指令中transclude選項及ng-transclude指令詳解
這篇文章主要研究一下如何使用ng-transclude指令,以及指令的transclude選項的相關(guān)資料,文中介紹的非常詳細,并給出了完整的示例代碼大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-05-05

