Also check: directive for form validation

User input validation is a core part of creating proper HTML forms. Form validators not only help you to get better quality data but they also guide the user through your form. Angular comes with a series of built-in validators such as required or maxLength etc. But very soon you have to build your own custom validators to handle more complex scenarios. In this lesson you're going to learn how to create such custom validators for Angular's reactive forms.

Basic validator is just a function.

import { ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms';

export function nameValidator(name: string): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const isValid = control.value === '' || control.value === name; if (isValid) {
return null;
} else {
return {
nameMatch: {
allowedName: name
}
};
}
};
}

Then you can use it with the validation in Reactvie form:

   import { nameValidator } from './name.validator';

    this.form = this.fb.group({
firstname: ['', [Validators.required, nameValidator('someone')]]
});

[Angular] Create a custom validator for reactive forms in Angular的更多相关文章

  1. [Angular] Create a custom validator for template driven forms in Angular

    User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...

  2. Angular之响应式表单 ( Reactive Forms )

    项目结构 一 首页 ( index.html ) <!doctype html> <html lang="en"> <head> <met ...

  3. [Angular] Create a custom pipe

    For example we want to create a pipe, to tranform byte to Mb. We using it in html like: <div> ...

  4. [Angular2 Form] Angular 2 Template Driven Form Custom Validator

    In this tutorial we are going to learn how we can also implement custom form field validation in Ang ...

  5. Angular Reactive Forms -- Model-Driven Forms响应式表单

    Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 )  官方文档:https://v2.angul ...

  6. [Angular] Component architecture and Reactive Forms

    It it recommeded that when deals with form component, we can create a container component to hold st ...

  7. [Angular2 Form] Model Driven Form Custom Validator

    In this tutorial we are going to learn how simple it is to create custom form field driven validator ...

  8. Forms in Angular 2

    Input handling is an important part of application development. The ng-model directive provided in A ...

  9. Part 13 Create a custom filter in AngularJS

    Custom filter in AngularJS 1. Is a function that returns a function 2. Use the filter function to cr ...

随机推荐

  1. 记intel杯比赛中各种bug与debug【其三】:intel chainer的安装与使用

    现在在训练模型,闲着来写一篇 顺着这篇文章,顺利安装上intel chainer 再次感谢 大黄老鼠 intel chainer 使用 头一次使用chainer,本以为又入了一个大坑,实际尝试感觉非常 ...

  2. BZOJ4472

    某售货员小T要到若干城镇去推销商品,由于该地区是交通不便的山区,任意两个城镇 之间都只有唯一的可能经过其它城镇的路线. 小T 可以准确地估计出在每个城镇停留的净收 益.这些净收益可能是负数,即推销商品 ...

  3. 巧用MAC地址表

    对于身处网络环境的人来说,不少朋友应该遇到过这种的情况:某一个终端找不到接在了哪一个交换机口上,也不知道数据包怎样走的. ok,那么这时候MAC地址表就作用了,拿下图的实验环境(H3C)来说好了 环境 ...

  4. vue使用,问题

    参考链接:https://cn.vuejs.org/v2/guide/index.html *)[Vue warn]: Error in v-on handler: "TypeError: ...

  5. ZooKeeper 特性

    ZooKeeper 拥有一个层次的命名空间.(like distributed)       注意:ZooKeeper 中不许使用相对路径.   一    ZooKeeper 数据模型         ...

  6. [Python] Boolean Or "Mask" Index Arrays filter with numpy

    NumPy Reference: Indexing Integer array indexing Boolean array indexing Note: The expression a < ...

  7. quick 3.3载入Spine问题

    近期项目要升级到Quick 3.3, 而且使用了Spine作为动画编辑器, 在此把升级过程中.有关quick 3.3 载入Spine遇到的坑在此记录一下. 1.Spine版本号问题 首先Quick 3 ...

  8. Visual Code中的智能提示

    https://code.visualstudio.com/docs/editor/intellisense C# https://marketplace.visualstudio.com/items ...

  9. Python: PS 滤镜--波浪特效

    本文用 Python 实现 PS 滤镜的波浪特效,具体效果可以参考之前的博客 http://blog.csdn.net/matrix_space/article/details/42215221 im ...

  10. 简单缓存Cache

    接口 interface ICache { /// <summary> /// 添加 /// </summary> /// <param name="key&q ...