Angular4表單驗證代碼詳解
背景:
最近在itoo頁面調整的時候,發(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>
效果:
###:初始化,沒有輸入數據:
###:輸入數據,但是長度小于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.非法用戶:
總結
以上所述是小編給大家介紹的Angular4表單驗證代碼詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
angular 實現(xiàn)的輸入框數字千分位及保留幾位小數點功能示例
這篇文章主要介紹了angular 實現(xiàn)的輸入框數字千分位及保留幾位小數點功能,涉及AngularJS數值運算、正則匹配等相關操作技巧,需要的朋友可以參考下2018-06-06Angular中的$watch、$watchGroup、$watchCollection
這篇文章主要介紹了Angular中的$watch、$watchGroup、$watchCollection ,需要的朋友可以參考下2017-06-06解決angularJS中input標簽的ng-change事件無效問題
今天小編就為大家分享一篇解決angularJS中input標簽的ng-change事件無效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09Angular js 實現(xiàn)添加用戶、修改密碼、敏感字、下拉菜單的綜合操作方法
這篇文章主要介紹了Angular js 實現(xiàn)添加用戶、修改密碼、敏感字、下拉菜單的綜合操作方法,需要的朋友可以參考下2017-10-10