angular6 表单验证
这里使用的是模型驱动的表单
1、app.module.ts
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
...
imports: [ReactiveFormsModule, ...],
...
})
export class AppModule{
}
文件中加入了ReactiveFormsModule模块,它是使用模型驱动表单所必须的。
2、app.component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
name: string
score: number;
formData: any;
constructor() { }
ngOnInit() {
this.formData = new FormGroup({
name: new FormControl(this.name, Validators.compose([
Validators.required,
])),
score: new FormControl(this.score, Validators.compose([
Validators.required,
this.scoreValueValidator
]))
});
}
scoreValueValidator(control: FormControl): any {
if (control.value < 0 || control.value > 100) {
return { value: {info: '分数必须大于等于0,小于等于100'} };
}
}
onsubmit(data: any) {
this.name= data.name;
this.score = data.score
}
}
表单验证,可以使用内置的表单验证,也可以使用自定义验证方法。
(1) 内置表单验证。
Validators是angular内置的验证器模块,可以使用它实现强制字段、最小长度、最大长度和模式等,我这里设置了name和score是强制字段,当然,你可以加入Validators.minLength(6), Validators.maxLength(10),Validators.pattern("[^ @]*@[^ @]*")等等。
(2) 自定义表单验证。
scoreValueValidator是自定义的验证方法,用于验证分数是否大于等于0,小于等于100,传递的参数是当前需要验证的表单的FormControl,通过control.value可以拿到输入的分数值。
3、app.component.html
<div class="container">
<form [formGroup] = "formData" (ngSubmit) = "onsubmit(formData.value)">
<div class="form-group">
<label for="name">姓名</label>
<em>*</em>
<input type="text" class="form-control" formControlName="name" id="name">
<div [hidden]="formData.get('name').valid || formData.get('name').untouched" class="small">
<p [hidden]="!formData.hasError('required', 'threshold')">必填项</p>
</div>
</div>
<div class="form-group">
<label for="score">分数</label>
<em>*</em>
<input type="number" min="0" max="100" class="form-control" formControlName="score" id="score">
<div [hidden]="formData.get('score').valid || formData.get('score').untouched" class="small">
<p [hidden]="!formData.hasError('required', 'score')">必填项</p>
<p [hidden]="!formData.hasError('value', 'score')">{{formData.getError('value', 'score')?.info}}</p>
</div>
<button type="submit" [disabled]="!formData.valid" class="btn btn-sm btn-primary">提交</button>
</form>
</div>
页面中显示错误信息
对于提交按钮,我们已经在方括号中添加了disabled,它被赋予值 !formData.valid。因此,如果formData.valid无效,按钮将保持禁用状态,用户将无法提交它。
4、app.component.css
em {
color:red;
margin-left: 0.25em
}
.ng-touched:not(form),.ng-invalid:not(form) {
border: 1px solid #f00;
}
.ng-valid:not(form),.ng-untouched:not(form) {
border: 1px solid #ddd;
}
p{
color:#f00;
}
angular6 表单验证的更多相关文章
- jQuery学习之路(8)- 表单验证插件-Validation
▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...
- 玩转spring boot——AOP与表单验证
AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...
- form表单验证-Javascript
Form表单验证: js基础考试内容,form表单验证,正则表达式,blur事件,自动获取数组,以及css布局样式,动态清除等.完整代码如下: <!DOCTYPE html PUBLIC &qu ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(33)-MVC 表单验证
系列目录 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可或缺的,微软的东西还是做得比较人性化的,从webform到MVC,都做到了双向验证 单单的用js实现的前端 ...
- 实现跨浏览器html5表单验证
div:nth-of-type(odd){ float: left; clear: left; } .origin-effect > div:nth-of-type(even){ float: ...
- jQuery Validate 表单验证 — 用户注册简单应用
相信很多coder在表单验证这块都是自己写验证规则的,今天我们用jQuery Validate这款前端验证利器来写一个简单的应用. 可以先把我写的这个小demo运行试下,先睹为快.猛戳链接--> ...
- jquery validate表单验证插件-推荐
1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家. 1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...
- 表单验证插件之jquery.validate.js
提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...
- 走进AngularJs 表单及表单验证
年底了越来越懒散,AngularJs的学习落了一段时间,博客最近也没更新.惭愧~前段时间有试了一下用yeoman构建Angular项目,感觉学的差不多了想做个项目练练手,谁知遇到了一系列问题.yeom ...
随机推荐
- centos 7下配置阿里yum源
1.打开centos的yum文件夹 cd /etc/yum.repos.d/ 2.用wget下载repo文件 wget http://mirrors.aliyun.com/repo/Centos-7. ...
- 【bzoj1997】[Hnoi2010]Planar(平面图+2-sat)
传送门 几乎和这个题一样,就不说题意了,比较特殊的点就是,这里有个结论: 平面图的边数\(m<3n-6\),\(n\)为点数. 所以我们可以通过这个减枝,\(m\)较大时直接输出\(no\).小 ...
- zabbix(LNMP)的企业微信告警
一.简介 KVM+虚拟机的基于LNMP平台zabbix3.0的监控系统.能通过企业微信实现服务器状态的告警功能! 二.环境 服务器:DELL 710 32G RIDA 5 系统:Linux 3. ...
- CF1185F Two Pizzas
CF1185F Two Pizzas 洛谷评测传送门 题目描述 A company of nn friends wants to order exactly two pizzas. It is kno ...
- MySQL 行格式
以 MySQL 默认的存储引擎 InnoDB 为例 InnoDB 包含以下四种行格式 Compact Redundant Dynamic Compressed 指定行格式 CREATE TABLE 表 ...
- win10 去除快捷方式小箭头
@echo off color 2 reg delete HKCR\lnkfile /v IsShortcut /f reg delete HKCR\piffile /v IsShortcut /f ...
- AtCoder Beginner Contest 139F Engines
链接 problem 给出\(n\)个二元组\((x,y)\).最初位于原点\((0,0)\),每次可以从这\(n\)个二元组中挑出一个,然后将当前的坐标\((X,Y)\)变为\((X+x,Y+y)\ ...
- TreeMap 原理
基于jdk1.8 TreeMap第一个想到的就是有序,当然也不是线程安全 TreeMap实现NavigableMap接口,说明支持一系列的导航方法 一.构造方法 public TreeMap() { ...
- 前端实现的canvas支持多图压缩并打包下载的工具
# 技术栈 canvas jszip.js(网页端压缩解压缩插件JSZIP库) FileSaver.js(文件保存到本地库) 在线预览:http://htmlpreview.github.io/?ht ...
- 使用Node.js时如何引入jQuery
使用Node.js时如何引入jQuery 首先安装jQuery依赖 npm install jquery 然后安装jsdom npm install jsdom 引入jQuery 新版正确的依赖方式 ...