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. 使用路径arc-奥运五环

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  2. iOS UI-微博案例(通过代码自定义Cell)

    一.Model BWWeiBo数据模型 #import <Foundation/Foundation.h> @interface BWWeiBo : NSObject @property ...

  3. golang channel本质——共享内存

    channel是golang中很重要的概念,配合goroutine是golang能够方便实现并发编程的关键.channel其实就是传统语言的阻塞消息队列,可以用来做不同goroutine之间的消息传递 ...

  4. js数组去重的几种方法

    1.遍历数组法 最简单的去重方法, 实现思路:新建一新数组,遍历传入数组,值不在新数组就加入该新数组中:注意点:判断值是否在数组的方法“indexOf”是ECMAScript5 方法,IE8以下不支持 ...

  5. mkfs.ext3 option

    mkfs.ext3 OPTIONS -b block-size Specify  the  size  of blocks in bytes.  Valid block-size values are ...

  6. HDU 2767 Proving Equivalences (Tarjan)

    Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  7. Android中判断字符是否为中文、韩文、日文

    我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...

  8. SharePoint Excel Service-PowerShell

    1. 创建一个信任的文件路径 New-SPExcelFileLocation -Address "http://sp2010/Excel Workbooks" -ExcelServ ...

  9. Java Socket 实现HTTP服务器核心

    原文链接:http://www.ihuxu.com/p/235.html   首先了解下HTTP协议: wikiPedia的说明很好,在此不重复了.链接:http://zh.wikipedia.org ...

  10. Tesseract-OCR 训练教程(一)

    实际应用中[font]替换为你自己的字体名,比如newfont.hehe等 1.根据tif生成box文件(位置宽高等信息)tesseract [font].font.exp0.tif [font].f ...