Create a custom validtor which only accepts the string start with '123';

    function skuValidator(control){
if(!control.value.match(/^123/)){
return {invalidSku: true};
}
}

If it not start with 123, then return the object {invalidSku: true}, which later will be used in the html.

To use this validtor:

        this.myForm = fb.group({
"sku": ["", Validators.compose([
Validators.required,
skuValidator
])]
});

Use Validators.compose([...]) to accpet mutli valiators.

In HTML:

            <div *ng-if="sku.control.hasError('invalidSku')">
SKU is required
</div>

Code:

import {Component, View, FORM_DIRECTIVES, Validators, FormBuilder, NgIf} from 'angular2/angular2';

@Component({
selector: 'demo-form-sku'
})
@View({
directives: [FORM_DIRECTIVES, NgIf],
template: `
<div>
<h2>Demo Form: Sku</h2>
<!-- ngForm is attched to the form, and #f="form" form is also come from ngForm-->
<form [ng-form-model]="myForm"
(submit)="onSubmit(myForm.value)">
<div class="form-group" [class.has-error]="!sku.valid && sku.touched">
<label for="skuInput">SKU</label>
<input type="text"
class="form-control"
id="skuInput"
#sku = "form"
placeholder="SKU"
[ng-form-control]="myForm.controls['sku']">
</div>
<div *ng-if="!sku.control.valid"
class="bg-warning">SKU is invalid</div>
<button type="submit" class="btn btn-default">Submit
</button>
<div *ng-if="sku.control.hasError('invalidSku')">
SKU is required
</div> </form>
<div *ng-if="!myForm.valid"
class="bg-warning">Form is invalid</div>
</div>
`
}) export class DemoFormSku {
myForm: ControlGroup;
constructor(fb:FormBuilder) {
this.myForm = fb.group({
"sku": ["", Validators.compose([
Validators.required,
skuValidator
])]
});
this.sku = this.myForm.controls['sku'];
} onSubmit(value){
console.log(value);
} function skuValidator(control){
if(!control.value.match(/^123/)){
return {invalidSku: true};
}
}
}

[Angular 2] Custom Validtors的更多相关文章

  1. [Angular] Http Custom Headers and RequestOptions

    updatePassenger(passenger: Passenger): Observable<Passenger> { let headers = new Headers({ 'Co ...

  2. [Angular] Create custom validators for formControl and formGroup

    Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...

  3. [Angular] Read Custom HTTP Headers Sent by the Server in Angular

    By default the response body doesn’t contain all the data that might be needed in your app. Your ser ...

  4. [Angular 8] Custom Route Preloading with ngx-quicklink and Angular

    In a previous lesson we learned about implementing a custom preloading strategy. That gives you a lo ...

  5. 来自 Thoughtram 的 Angular 2 系列资料

    Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...

  6. Angular vs React---React-ing to change

    这篇文章的全局观和思路一级棒! The Fairy Tale Cast your mind back to 2010 when users started to demand interactive ...

  7. Ionic + AngularJS

    Ionic Framework Ionic framework is the youngest in our top 5 stack, as the alpha was released in lat ...

  8. angular custom Element 自定义web component

    angular 自定义web组件: 首先创建一个名为myCustom的组件. 引入app.module: ... import {customComponent} from ' ./myCustom. ...

  9. [Angular] Angular Custom Change Detection with ChangeDetectorRef

    Each component has its own ChangeDetectorRef, and we can inject ChangeDetectorRef into constructor: ...

随机推荐

  1. C# Winform 双屏显示

    双屏显示1 // 利用WinForm中的Screen类,即可比较方便地实现多窗体分别在多个屏幕上显示. //•获取当前系统连接的屏幕数量: Screen.AllScreens.Count(); //• ...

  2. java 全角、半角字符串转换

    转自:http://www.cnblogs.com/modou/articles/2679815.html     加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...

  3. phpstorm IDE编辑器使用手记

    a.选项卡限制问题 突破打开的文件选项卡个数限制:默认安装后的phpstorm打开的文件个数是有限制的,默认是10个.也就是最多同时打开10个文件进行编辑. 超过10个就会自动关闭前面最先打开的文件. ...

  4. jquery 判断页面滚动到底部

    $(document).scrollTop() 获取垂直滚动的距离 即当前滚动的地方的窗口顶端到整个页面顶端的距离$(document).scrollLeft() 这是获取水平滚动条的距离获取顶端 只 ...

  5. 安卓AVD使用建议

    问题描述:之前在安装了Android开发环境后,一开始并没有直接在Android手机和平板上进行调试,是使用的AVD模拟器工具.由于电脑的配置不是特别好,总感觉AVD的使用速度太慢,包括启动的时候还有 ...

  6. Mvc controller单元测试 Mock Url对象

    被测试Action 包含有Url对象的代码: data = new data { title = ds.Name, icon = "folder", attr = new { id ...

  7. 《30天自制操作系统》读书笔记(5) GDT&IDT

    梳理项目结构 项目做到现在, 前头的好多东西都忘了, 还是通过Makefile重新理解一下整个项目是如何编译的: 现在我们拥有这么9个文件: ipl10.nas    InitialProgramLo ...

  8. eclipse问题解决(link方式安装插件失败)

    使用 link 方式,离线安装 eclipse 插件时,经常失败. 一.常见的失败情况 link方式配好后,eclipse 启动,没有弹出任何信息. 查看当前工作空间——.metadata——.log ...

  9. 如何把iOS代码编译为Android应用

    新闻 <iPhone 6/6 Plus中国销量曝光:单月销量650万>:据iSuppli Corp.中国研究总监王阳爆料,iPhone 6和iPhone 6 Plus在国内受欢迎的情况大大 ...

  10. Blue Jeans

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...