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


2.2.多規則驗證:(不能為空,用戶名和密碼的長度)
<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>效果:
###:初始化,沒有輸入數據:

###:輸入數據,但是長度小于3:

###:合法輸入:
 
 
  當然這里有一個問題,就是合法的時候usernameRef.errors=null,但是用戶看起來不太美觀,所以就需要判斷當usernameRef.errors=null的時不出現:
<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.非法用戶: 

總結
以上所述是小編給大家介紹的Angular4表單驗證代碼詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
新聞熱點
疑難解答