源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Angular4表单验证代码详解

  • 时间:2022-09-29 20:06 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Angular4表单验证代码详解
[b] 背景:[/b]      最近在itoo页面调整的时候,发现页面表单或者是文本框没有做基本的判断操作,所以着手demo一篇,希望对大家有帮助!! -------------------------------------------------------------------------------- [b]1.创建表单组件:[/b] [code]ng g c login1[/code] [b]2.1单规则验证:[/b]
<label>用户名:</label>
 <input type="text" #userNameRef=ngModel [(ngModel)]=userName required>
 <span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.valid}}</span>
-------------------------------------------------------------------------------- 效果: [img]http://files.jb51.net/file_images/article/201709/201709030959037.jpg[/img] [img]http://files.jb51.net/file_images/article/201709/201709030959038.jpg[/img] [b]2.2.多规则验证:(不能为空,用户名和密码的长度)[/b]
<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>
效果: ###:初始化,没有输入数据: [img]http://files.jb51.net/file_images/article/201709/201709030959049.jpg[/img] ###:输入数据,但是长度小于3: [img]http://files.jb51.net/file_images/article/201709/2017090309590410.jpg[/img] ###:合法输入: [img]http://files.jb51.net/file_images/article/201709/2017090309590411.jpg[/img]     当然这里有一个问题,就是合法的时候[code]usernameRef.errors=null,[/code]但是用户看起来不太美观,所以就需要判断当[code]usernameRef.errors=null[/code]的时不出现:
<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("非法用户");
 }
 }
}
[b]3.userModel:[/b]
export class UserModel{
 userName:string;
 password:string;
}
效果: 1.未填时点击登陆: [img]http://files.jb51.net/file_images/article/201709/2017090309590412.jpg[/img]   2.输入登陆: [img]http://files.jb51.net/file_images/article/201709/2017090309590413.jpg[/img]   3.非法用户:  [img]http://files.jb51.net/file_images/article/201709/2017090309590414.jpg[/img] 总结 以上所述是小编给大家介绍的Angular4表单验证代码详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部