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

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

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

To use this validtor:

  1. this.myForm = fb.group({
  2. "sku": ["", Validators.compose([
  3. Validators.required,
  4. skuValidator
  5. ])]
  6. });

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

In HTML:

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

Code:

  1. import {Component, View, FORM_DIRECTIVES, Validators, FormBuilder, NgIf} from 'angular2/angular2';
  2.  
  3. @Component({
  4. selector: 'demo-form-sku'
  5. })
  6. @View({
  7. directives: [FORM_DIRECTIVES, NgIf],
  8. template: `
  9. <div>
  10. <h2>Demo Form: Sku</h2>
  11. <!-- ngForm is attched to the form, and #f="form" form is also come from ngForm-->
  12. <form [ng-form-model]="myForm"
  13. (submit)="onSubmit(myForm.value)">
  14. <div class="form-group" [class.has-error]="!sku.valid && sku.touched">
  15. <label for="skuInput">SKU</label>
  16. <input type="text"
  17. class="form-control"
  18. id="skuInput"
  19. #sku = "form"
  20. placeholder="SKU"
  21. [ng-form-control]="myForm.controls['sku']">
  22. </div>
  23. <div *ng-if="!sku.control.valid"
  24. class="bg-warning">SKU is invalid</div>
  25. <button type="submit" class="btn btn-default">Submit
  26. </button>
  27. <div *ng-if="sku.control.hasError('invalidSku')">
  28. SKU is required
  29. </div>
  30.  
  31. </form>
  32. <div *ng-if="!myForm.valid"
  33. class="bg-warning">Form is invalid</div>
  34. </div>
  35. `
  36. })
  37.  
  38. export class DemoFormSku {
  39. myForm: ControlGroup;
  40. constructor(fb:FormBuilder) {
  41. this.myForm = fb.group({
  42. "sku": ["", Validators.compose([
  43. Validators.required,
  44. skuValidator
  45. ])]
  46. });
  47. this.sku = this.myForm.controls['sku'];
  48. }
  49.  
  50. onSubmit(value){
  51. console.log(value);
  52. }
  53.  
  54. function skuValidator(control){
  55. if(!control.value.match(/^123/)){
  56. return {invalidSku: true};
  57. }
  58. }
  59. }

[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. 『重构--改善既有代码的设计』读书笔记----Substitute Algorithm

    重构可以把复杂的东西分解成一个个简单的小块.但有时候,你必须壮士断腕删掉整个算法,用简单的算法来取代,如果你发现做一件事情可以有更清晰的方式,那你完全有理由用更清晰的方式来解决问题.如果你开始使用程序 ...

  2. Telerik_2012_Q3 破解全套下载链接

    1.Telerik_OpenAccess_ORM_2012_3_1012_SDK.zip (暂未提供下载) 2. Telerik_OpenAccess_ORM_2012_3_1012.zip 3. T ...

  3. Bootstrap_表单_图标

    在Bootstrap框架中是通过给元素添加“glyphicon”类名来实现,然后通过伪元素“:before”的“content”属性调取对应的icon编码: <span class=" ...

  4. jquery 自定义tab

    <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js&qu ...

  5. php 钩子函数原理 解析

    目前对钩子的理解:<转载:http://www.cnblogs.com/del/archive/2008/02/25/1080825.html> 譬如我们用鼠标在某个窗口上双击了一次, 或 ...

  6. nginx+uwsgi+WSGI applications

    uwsgi一个专业的部署运用的工具,不仅能够部署Python运用,还能够部署其他运用比如Perl,Ruby等 uWSGI 安装: pip install uwsgi WSGI application( ...

  7. BZOJ 3893 Cow Jog

    Description The cows are out exercising their hooves again! There are \(N\) cows jogging on an infin ...

  8. ubuntu删除openjdk,安装 Sun JDK

    1.到官网下载安装包: jdk-7-linux-i586.tar.gz 2.创建安装目录:sudo mkdir /usr/lib/jvm 3. 解压缩:tar zxvf ./jdk-7-linux-i ...

  9. CentOS 5升级Python版本(2.4>2.7)

    安装SALT时,需要这样作,公司有一批REDHAT5的,弄起来... 然后却是: Missing Dependency: python(abi) = 2.6 is needed by package ...

  10. 获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName

    http://blog.csdn.net/bichenggui/article/details/4774457 -------------------------------------------- ...