加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular4表单验证详解

发布时间:2020-12-17 08:55:51 所属栏目:安全 来源:网络整理
导读:背景: 最近在itoo页面调整的时候,发现页面表单或者是文本框没有做基本的判断操作,所以着手demo一篇,希望对大家有帮助!! 1.创建表单组件: ng g c login 2.1单规则验证: label 用户名: / label input type = "text" # userNameRef = ngModel [( ngMod

背景:
最近在itoo页面调整的时候,发现页面表单或者是文本框没有做基本的判断操作,所以着手demo一篇,希望对大家有帮助!!


1.创建表单组件:

ng g c login

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.非法用户:


结语:
感谢浏览,希望对你有帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读