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. dstat---统计磁盘,CPU,IO,等相关信息

    dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一个全能系统信息统计工具.与sysstat相比,dstat拥有一个彩色的界面,在手 ...

  2. Unity Shader (五)Surface Shader示例

    1.替换颜色 Shader "Custom/Example_Frag_5" { Properties { _MainTex ("Albedo (RGB)", 2 ...

  3. 欢迎各位技术牛人增加Swift QQ群:343549891

    急招:五年以上Swift开发经验,24个月工资.30天年假.配司机专车. 欢迎各位技术牛人增加Swift 敏捷大拇指 官方QQ群1: 报上"来自CSDN"就可以.谢谢! 訪问 大拇 ...

  4. Android Retrofit网络请求Service,@Path、@Query、@QueryMap、@Map...

    http://blog.csdn.net/jdsjlzx/article/details/51607867

  5. WebAssembly学习(三):AssemblyScript - TypeScript到WebAssembly的编译

    虽然说只要高级语言能转换成 LLVM IR,就能被编译成 WebAssembly 字节码,官方也推荐c/c++的方式,但是让一个前端工程师去熟练使用c/c++显然是有点困难,那么TypeScript ...

  6. 23种JavaScript设计模式

    原文链接:https://boostlog.io/@sonuton/23-javascript-design-patterns-5adb006847018500491f3f7f 转自: https:/ ...

  7. 学习《SQL必知必会(第4版)》中文PDF+英文PDF+代码++福达BenForta(作者)

    不管是数据分析还是Web程序开发,都会接触到数据库,SQL语法简洁,使用方式灵活,功能强大,已经成为当今程序员不可或缺的技能. 推荐学习<SQL必知必会(第4版)>,内容丰富,文字简洁明快 ...

  8. fgrep---指定的输入文件中的匹配模式的行

    fgrep命令是用来搜索 file 参数指定的输入文件(缺省为标准输入)中的匹配模式的行.fgrep 命令特别搜索 Pattern 参数,它们是固定的字符串.如果在 File 参数中指定一个以上的文件 ...

  9. 紫书 习题 10-18 UVa 10837 (欧拉函数变形)

    这道题很巧妙,要把式子变一下 phi(n) = n * (1 - 1 / p1) * (1 - 1 / p2)--(1 - 1 / pr) = n * ((p1-1) / p1) * ((p1-2) ...

  10. Word中查找替换软回车键和回车键

    在Word中使用搜索功能搜索“^p”组合字符串可以查找文档中的所有换行符(回车键),使用“^l”(英文输入状态下shift+6与小写字符L的组合)可以搜索所有的软回车符.使用替换功能就可以搜索替换二者 ...