Create a directive to check no special characters allowed:

import {Directive, forwardRef} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, Validator} from '@angular/forms';
@Directive({
selector: `[formControl][no-special-chars],
[formControlName][no-special-chars],
[ngModel][no-special-chars]`,
providers: [
{
multi: true,
provide: NG_VALIDATORS,
useExisting: forwardRef(() => NoSpecialCharsValidator)
}
]
}) export class NoSpecialCharsValidator implements Validator {
validate(c: AbstractControl): { [key: string]: any; } {
const res = /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(c.value);
return res ? {special: true}: null;
}
}
        <div class="meal-form__name">
<label>
<h3>Meal name</h3>
<input type="text"
no-special-chars
formControlName="name"
placeholder="e.g. English Breakfast">
<div class="error" *ngIf="noSpecials">
Cannot contain special characters
</div>
</label>
</div>
  get noSpecial() {
return (
this.form.get('name').hasError('special') &&
this.form.get('name').touched
);
}

[Angular] Custom directive Form validator的更多相关文章

  1. [Angular] Using directive to create a simple Credit card validator

    We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...

  2. [Angular 2] Directive intro and exportAs

    First, What is directive, what is the difference between component and directive. For my understandi ...

  3. Effective Java 75 Consider using a custom serialized form

    Principle Do not accept the default serialized form without first considering whether it is appropri ...

  4. 关于angular 自定义directive

    关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...

  5. [php基础]PHP Form表单验证:PHP form validator使用说明

    在PHP网站开发建设中,用户注册.留言是必不可少的功能,用户提交的信息数据都是通过Form表单提交,为了保证数据的完整性.安全性,PHP Form表单验证是过滤数据的首要环节,PHP对表单提交数据的验 ...

  6. [Angular] Test Directive

    directive: import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core'; @Direct ...

  7. [Angular] Export directive functionalities by using 'exportAs'

    Directive ables to change component behaives and lookings. Directive can also export some APIs which ...

  8. [Angular] @ViewChild read custom directive and exportAs

    For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...

  9. angular $http 与form表单的select-->refine

    <!DOCTYPE html> <html ng-app="a2_15"> <head> <meta http-equiv="C ...

随机推荐

  1. Integer 和 int的种种比较

    public static void main(String[] args) { int i = 128; Integer i2 = 128; Integer i3 = new Integer(128 ...

  2. 对Jscript操作注册表接口的一点不解

    作者:朱金灿 来源:http://blog.csdn.net/clever101 要操作注册表需要通过ActiveX控件调用WScript.shell对象,通过该对象的一些方法来操作.Wshshell ...

  3. Asp.Net碎知识

    在aspx页面 获取值: UserModel user=new UserModel();实例化 user.Address=context["txtAddress"]; 如果前台不需 ...

  4. Sub Thread to update main Thread (UI) 2

    Sub Thread to update main Thread (UI)  2 Handler.post(somethread); Handler.sendMessage("Msg&quo ...

  5. Ace在线编辑器使用requirejs配置

    Ace代码在线编辑器如果需要在requirejs里使用,注意需要使用github上lib/ace目录的文件. 如果使用ajaxorg/ace-builds下面的代码再使用requirejs会报错,不能 ...

  6. CSUOJ 1555 Inversion Sequence

    1555: Inversion Sequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 107  Solved: 34 Description ...

  7. 3D图形处理库

    转自 3D图形处理库 高性能软件光栅化渲染器 OpenSWR OpenSWR —— 用于OpenGL的高性能,高度可扩展的软件光栅化渲染器 OpenSWR的目的是提供一个高性能,高度可扩展的OpenG ...

  8. 【v2.x OGE教程 12】 关卡编辑器帮助文档

    ] 关卡编辑器帮助文档 一.简单介绍 关卡编辑器用于游戏关卡界面元素的可视化编辑,包含元素的位置.尺寸以及其他自己定义属性.通过解析生成的数据文件就可以获取关卡信息,并能随时调整.以降低开发工作量,提 ...

  9. java使用默认线程池踩过的坑(二)

    云智慧(北京)科技有限公司 陈鑫 是的.一个线程不可以启动两次.那么它是怎么推断的呢? public synchronized void start() { /** * A zero status v ...

  10. 43.安装npm及cnpm(Windows)

    转自:https://www.cnblogs.com/yominhi/p/7039795.html [工具官网] Node.js : http://nodejs.cn/ 淘宝NPM: https:// ...