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. Android View体系(十)自定义组合控件

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

  2. BZOJ 3940 AC自动机

    思路: 需要维护一个栈的AC自动机--. 要求出来 最后的栈顶是在自动机上的哪个节点. if(!ac.ch[st[tp-1]][a[i]-'a']) st[tp]=ac.ch[ac.f[st[tp-1 ...

  3. BZOJ 1103 DFS序+线段树

    思路: 先搞出来DFS序 进入这个点 +1 出这个点 -1 线段树维护前缀和 (因为还要修改) 搞定 修改的时候只修改底下节点就OK了 (边权–>点权 不多说) //By SiriusRen # ...

  4. PL/SQL Developer怎么连接远程数据库

    首先打开电脑,到PL/SQL安装的指定目录 [D:\app\DZL\product\10.2.0\dbhome_1\NETWORK\ADMIN]或者[D:\oracle\product\10.2.0\ ...

  5. sql 通过某段时间求得改段时间内的工作时长,排除工作日

    CREATE FUNCTION Fun_GetTotalHourBySomeTime(@TaskId NVARCHAR(30),@Bu_trupstartDate NVARCHAR(50),@Bu_t ...

  6. CentOS上搭建Tomcat环境并配置服务自启动

    下载安装JDK 卸载原装的OpenJDK(如果有) # 查看是否安装Java java -version # 查看Java的安装包信息 rpm -qa | grep java # 卸载原装Java,& ...

  7. pip-window安装

    windows 安装: 保证计算机联网直接使用cmd 执行 python -m pip install -U pip 自动安装 找到 python安装的路径 C:\Users\Administrato ...

  8. 转载——利用C#自带组件强壮程序日志

    利用C#自带组件强壮程序日志   在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...

  9. js---对象 和 函数this

    一:对象创建的方法 //普通 字面量形式 var obj = { name:'名字', fn:function(){ console.log(this.name); } } //new 实例 var ...

  10. 8.ZOrder

    T3LayerZorder.h #pragma once #include "cocos2d.h" USING_NS_CC; class T3LayerZorder:public ...