1.html

<form [formGroup]="formModel" (submit)="submit()">
<div>
用户名:<input type="text" formControlName="username">
电话:<input type="text" formControlName="mobile"> <div formGroupName="passwordsGroup">
密码:<input type="password" formControlName="password">
确认密码:<input type="password" formControlName="pconfirm">
</div> </div>
<div><button type="submit">保存</button></div>
</form>

  

2. 控制器中创建FormModel

  constructor(private http: Http, fb: FormBuilder) {
this.formModel = fb.group({
username: ['', [Validators.required, Validators.minLength(10)]],
mobile: ['', this.mobileValidator],
passwordsGroup: fb.group({
password: [''],
pconfirm: ['']
}, { validator: this.equalValidator} ),
});
}

  

3. 创建自定义校验器

  mobileValidator(control: FormControl): any {
const myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
const valid = myreg.test(control.value);
console.log('mobile result: ' + valid);
return valid ? null : {mobile: true}; } equalValidator(group: FormGroup): any {
const password: FormControl = group.get('password') as FormControl;
const pconfirm: FormControl = group.get('pconfirm') as FormControl;
const valid: boolean = password.value === pconfirm.value;
console.log('password equal: ' + valid);
return valid ? null : {equal: true};
}

  mobileValidator用来校验电话号码是否有效

  equalValidator 用来校验两次输入的密码是否一致

4. submit方法

  submit() {
const isValid: boolean = this.formModel.get('username').valid;
console.log('username: ' + isValid);
const errors: any = this.formModel.get('username').errors;
console.log('username errors' + JSON.stringify(errors));
if ( this.formModel.valid) {
console.log(this.formModel.value);
} }

  

5.完整的代码

Angular 4 表单校验1的更多相关文章

  1. Angular 4 表单校验2

    1. 将表单的方法移动到单独的ts文件夹中 2. code export function mobileValidator(control: FormControl): any { const myr ...

  2. angularJs表单校验(超级详细!!!)

    html代码 <!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> < ...

  3. angular js 表单验证

    <!doctype html> <html ng-app="myapp"> <head> <meta charset="UTF- ...

  4. angular之表单验证与ngMessages

    刚接触angular1.x很多经常用到的ngMessages的地方,这里顺便记一下,效果如下图: 如果引用了angular-messages.js报如下错误,说明你的angular.js和angula ...

  5. AngularJs轻松入门(六)表单校验

    表单数据的校验对于提高WEB安全性意义不大,因为服务器接收到的请求不一定来自我们的前端页面,有可能来自别的站点,黑客可以自己做一个表单,把数据提交到我们的服务器(即跨站伪造请求),这样就绕过了前端页面 ...

  6. ng-messages AngularJs 表单校验方式

    最新研究了一下表单校验,直接上代码. <!DOCTYPE html><html ng-app='app'><head> <meta charset=" ...

  7. 利用jquery.validate以及bootstrap的tooltip开发气泡式的表单校验组件

    表单校验是页面开发中非常常见的一类需求,相信每个前端开发人员都有这方面的经验.网上有很多成熟的表单校验框架,虽然按照它们默认的设计,用起来没有多大的问题,但是在实际工作中,表单校验有可能有比较复杂的个 ...

  8. AngularJS 1.2.x 学习笔记(表单校验篇)

    https://my.oschina.net/cokolin/blog/526911 摘要: 本文首发于 blog.csdn.net/vipshop_ebs/article/details/39472 ...

  9. bootstrap+jQuery.validate表单校验

    谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.User是我们面对较多的对象,也是较核心的对象.最开始的用户注册和登 ...

随机推荐

  1. 使用pthread_create()创建线程

    可以通过 pthread_create()函数创建新线程. #include <pthread.h> int pthread_create(pthread_t *restrict tidp ...

  2. jconsole工具使用----jvm内存泄漏问题

    转载地址:https://www.cnblogs.com/baihuitestsoftware/articles/6405580.html Jconsole,Java Monitoring and M ...

  3. Taffy自动化测试框架Web开发,Python Flask实践详解

    1. 前言 最近为Taffy自动化测试框架写了个页面,主要实现了用例管理.执行,测试报告查看管理.发送邮件及配置等功能.   本页面适用所有基于taffy/nose框架编写的自动化测试脚本,或基于un ...

  4. TClientDataSet的 AddIndex

    unit Unit2; interface uses SysUtils, Classes, DB, DBClient; type TDataModule2 = class(TDataModule) C ...

  5. JS底层知识理解之执行上下文篇

    JS底层知识理解之执行上下文篇 一.什么是执行上下文(Execution Context) 执行上下文可以理解为当前代码的执行环境,它会形成一个作用域. 二.JavaScript引擎会以什么方式去处理 ...

  6. python 模块化管理,导入自己的模块(包)

    很多东西似懂非懂,或者当时看懂了,但是感觉不是很重要,经常不用,时间久了,也就变得似懂非懂了.今天被某度电话面试问道一个问题,就是模块倒入,其实我之前也是很仔细的研究过的,不过由于平时做的东西太简单, ...

  7. <NET CLR via c# 第4版>笔记 第18章 定制特性

    18.1 使用定制特性 FCL 中的几个常用定制特性. DllImport 特性应用于方法,告诉 CLR 该方法的实现位于指定 DLL 的非托管代码中. Serializable 特性应用于类型,告诉 ...

  8. IoC基础例子

    一个简单的例子: 一般新建一个com.dao包,存放一些dao接口. com.dao.impl里面存放具体的dao com.service存放service接口 com.service.impl具体的 ...

  9. 相同类型的对象不能互相转换:java.lang.ClassCastException: com.anhoo.po.UserPo cannot be cast to com.anhoo.po.UserPo

    @PostMapping("/findone") public String getone(UserVo userVo) throws IllegalAccessException ...

  10. AngularJS-----$compile

    原文:http://docs.ngnice.com/api/ng/service/$compile 写在前面的话: 之前我一直理解错误,我一直以为这句--function([scope], clone ...